C++ default parameter questions

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

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 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}

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 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 on other sites
Quote:
 Original post by justinrwalshWell 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.

1. 1
Rutin
26
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• Forum Statistics

• Total Topics
631751
• Total Posts
3002086
×