Sign in to follow this  
justinrwalsh

C++ default parameter questions

Recommended Posts

Well here it is. i know this is valid code...
int myFunc(int i=400, int j=2);
so the quesion lies in something kinda like this but in a class... So basically here is what i am trying...

class SuperDuper  {
public:

SuperDuper(void);
void TheSpecialFunction(int myVal=protectedVal);

protected:

int protectedVal;
int myOtherValue;
int superLongNameForAnInteger;
};
It doesn't work. See i have this function called SetVelociy(x,y) and what if the user doesnt want to change the velocity of the x just they y? i could always make it two seperate functions, but is there a way to just except nothing passed to the x, but still change the y? do i mkae any sorts of sense? please, any input is welcome!

Share this post


Link to post
Share on other sites
You can do it for class methods in the same way as you do for functions.


class Foo {
public:
Foo( int x = 100 );
void doStuff( int x = 200, int y = 300 );
};

Foo::Foo( int x ) { // No default values here!
// Stuff
}

int Foo::doStuff( int x, int y ) { // No default values here!
// Other stuff
}




[Edit]
About your other question: No, you can't change the y and not change the x. Like in doStuff, default values fill from right to left, so to set y you must set x.

Share this post


Link to post
Share on other sites
I would suggest you just go with separate methods, a lot less headache that way :) If you try to just set the y-value in a function like that, it won't work, as the parameter types are the same and x comes first in the parameter listing, it will be assumed to be the value passed. As for using a member variable as default value, AFAIK only constants are allowed as the compiler replaces the function call when using default parameters, and not the function itself. So calling a function defined as int func( int val = 0 ); with int var = func(); would simple be converted into int var = func( 0 ); Hope that helped :)

Share this post


Link to post
Share on other sites
Quote:
Original post by justinrwalsh
Well here it is.

i know this is valid code...


int myFunc(int i=400, int j=2);


so the quesion lies in something kinda like this but in a class...

So basically here is what i am trying...


class SuperDuper {
public:

SuperDuper(void);
void TheSpecialFunction(int myVal=protectedVal);

protected:

int protectedVal;
int myOtherValue;
int superLongNameForAnInteger;
};


It doesn't work.


See i have this function called SetVelociy(x,y) and what if the user doesnt want to change the velocity of the x just they y? i could always make it two seperate functions, but is there a way to just except nothing passed to the x, but still change the y?

do i mkae any sorts of sense?


please, any input is welcome!


Default parameter values must be constant, the protectedVal you try to use is a non-constant member variable and thus cannot be used for that. If you wish to provide functionality to not change the protectedVal I would suggest using method overloading, which is, in many cases, a wonderfully clean way to provide versatile functionality. Implement in lieu with:


void TheSpecialFunction() { TheSpecialFunction(protectedVal); }
void TheSpecialFunction(int someValue);


Neat. Pretty. Good.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this