Jump to content
  • Advertisement
Sign in to follow this  
Telastyn

Complex Default Parameters?

This topic is 5123 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

Using C++, programmers are able to specify default parameters for functions should the parameter not be specified:
void printint(int foo=5){
    cout << foo << "\n";
}

printint();
psuedo code would print 5 for example. What [if any] formal limitations apply to the assignment there? For example, the following code actually compiles nicely and does 'as expected':
int bar(){
    return(5);
}

void foo(int f=bar()){
    cout << f << "\n";
}

void main(){
    foo();
}

Share this post


Link to post
Share on other sites
Advertisement
you cant do:
void func(int a=1,int b);
/* or */
void func(int a=1,int b,int c=2);
/* or */
void func(int a=1);
func(int a=1) {} /* redefinition error,
the default parameter must be in the prototype OR function definition.
cant be in both */

but i think this will compile fine:
void func(int a=1,int b);
func(int a,int b=2) {}

Share this post


Link to post
Share on other sites
Quote:
Original post by pex22
but i think this will compile fine:
void func(int a=1,int b);
func(int a,int b=2) {}
Nope. "error C2548: 'func' : missing default parameter for parameter 2". The compiler needs to know the default arguments when the call is made, so they can be passed if the code doesn't supply any default args.

Share this post


Link to post
Share on other sites
Hrm, I wonder if my compiler simply replaced bar in the example code with 5. I can't seem to find a nice definative answer online. Most places seem to imply that it should not work.

Share this post


Link to post
Share on other sites
You have to supply default parameters for all of the params from the first with a default param through to the last param.

void func(int a, int b, int c=1); // OK
void func(int a, int b=1, int c=2); // OK
void func(int a, int b=1, int c); // ERROR
void func(int a=1, int b, int c=2); // ERROR

As for what's valid as part of the assignment, I think that anything that is valid for defining a local variable will also be valid for defining a default parameter. Although, I'm not 100% sure on this. Best way to find out is try it out.

Well, actually the best way is to go read the standard, second best way is to try it out. [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Hrm, I wonder if my compiler simply replaced bar in the example code with 5. I can't seem to find a nice definative answer online. Most places seem to imply that it should not work.

Try this:

void foo(int f=rand())
{
std::cout << f << std::endl;
}

Share this post


Link to post
Share on other sites
heh, yes yes, I know the limitations to normal param=constant default params. I was suprised that using a function's return value as a default parameter worked.

Anyways, I have found a more definative answer from msdn:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/ators_32.asp

"The expression can combine functions that are visible in the current scope, constant expressions, and global variables. The expression cannot contain local variables or non-static class member variables."

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
"The expression can combine functions that are visible in the current scope, constant expressions, and global variables. The expression cannot contain local variables or non-static class member variables."

Makes sense, since this isn't in scope yet. In the same way you can't do
void func(int a, int b=a);

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!