Jump to content
  • Advertisement
Sign in to follow this  
2DamnFunky

default arguments and arrays

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive searched for this in my book and on this site with the google search but no luck. Im presuming impossible but would appreciate work arounds. If a class has a private int array member how would you use a constructur with default arguments to initialise the array when an object is created. i.e thing(int a = 0, char * str = "Hello", int *array = ??? ) //constructor

Share this post


Link to post
Share on other sites
Advertisement
OK, have two constructors:

thing(int a = 0, char * str = "Hello")
and
thing(int *array, int a = 0, char * str = "Hello")

Have the one without the "array" argument initialize the default array, otherwise use whatever the user gives you.


Of course, using this method, the user is free to pass in any ol' pointer to any ol' integer (not necessaraly an array).

Funnly enough, I've never come across a problem like this - so I can't give you an answer. I generally use std::vector or std::list when I need this type of functionality. Arrays are yucky [smile].


Also, your "char*" argument should be "const char*"... or std::string.

Share this post


Link to post
Share on other sites
Do you know the size of the array?

I would think something like:


thing(int a = 0, char * str = "Hello", int array[6] = {0,1,2,3,4,5} )


would work. I haven't tested it though, never come across this problem either.

Share this post


Link to post
Share on other sites
thing(int a = 0, std::string str = "Hello", std::vector< int > array = defaultThingVector)

where defaultThingVector is a static member of the class.

Enigma

Share this post


Link to post
Share on other sites
Thanks for the advice. Judging by the responses of "never seen this before" im guessing its my sloppy class design so ill use your reponses but ill think if theres something obviously more simple im overlooking. Thanks for this tips about const and std:: however i dont know anything "YET" about std:: (next chapter!!)

thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!