Func Overloading

Suppose there is a function with following prototype :

void f(int=10,int=20,int=30,int=40)

If this function is called by passing by two arguments to it , is there any way we can make sure that these arguments are treated as first and third arguments (whereas second and fourth are taken as defaults) ?

Not with that function alone, no.

You could do this, though:

1
2
3
4
5
6
void f(int,int,int,int);  // note:  no defaults

void f(int a=10,int c=30)
{
  f(a,20,c,40);
}



But make sure this is wise. It might be more confusing than helpful.
No. There is not. You can experiment with function overloading, which might let you have a bit of leeway, but ultimately you can't have ambiguous function calls, and if you write
f(4, 5);
then the compiler would not possibly be able to know whether to call
f(4, 5, 30, 40);
or
[/code]f(4, 20, 5, 40);[/code]
Thus you cannot do this.

How about a function with a different name:
 
void f_simple(int a, int c) { f(a, 20, c, 40); }
@Disch : No no its not confusing its helpful thanks , but isn't there any way to directly access that function (without using another function).
Give them different names, as I suggested :) I guess you could use templates if you wanted to keep the name notionally the same, but that's probably a bad idea.
@Xander @Disch : Ok thanks i got it :)
The named parameter idiom is another possible workaround

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.20
@mik2718 : its bit tough , but i'll try :) Thanks a lot
Last edited on
Topic archived. No new replies allowed.