Sign in to follow this  

Complex Default Parameters?

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

This topic is 4816 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.

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