Optional arguments
-
oops - my mistake... I meant to declare the function: void func(int A, int B, int C, int D=-1, int E=-1, F=-1); but if I declare it this way, will I be able to call the func without specifying the parameters D and E?
SWDevil wrote:
but if I declare it this way, will I be able to call the func without specifying the parameters D and E?
Well if you are desperate to get it working take a look at
va_arg
,va_list
,va_start
,va_end
. It's an option.
Owner drawn Jesus Loves
-
oops - my mistake... I meant to declare the function: void func(int A, int B, int C, int D=-1, int E=-1, F=-1); but if I declare it this way, will I be able to call the func without specifying the parameters D and E?
No you can't :-( What you want is calling the function
void func(int A, int B, int C, int D=-1, int E=-1, F=-1);
infunc(A,B,C,,,F);
Suppose( which will never happen)if you succeed, you will get D and E as -1. So you can callfunc(A, B, C, -1, -1, F); :-)
Regards Anil -
Hi, I have a function that is declared as follows: void func(int A, int B, int C, int D=-1, int E=-1); arguments D and E are optional, and so if the function is called like this: func(A,B,C); then D and E recieve the value -1. What I want to do is add another optional argument, let's say int F that will also receive -1 as default, for example. The problem is that sometimes I want to call the function only with the parameters A,B,C and F. If I declare the function like this: void func(int A, int B, int C, int D=-1, int E=-1, F); will I be able to call the function in this manner: func(A,B,C,,,F); ?
why not putting the
F
parameter betweenC
andD
?void foo(int A, int B, int C, int F = -1, int D = -1, int E = -1) {
//...
}so that you can use it like this :
int A, B, C, D, E, F;
foo(A, B, C); // A B C -1 -1 -1
foo(A, B, C, F); // A B C -1 -1 F
foo(A, B, C, F, D); // A B C D -1 F
foo(A, B, C, F, D, E); // A B C D E F
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
why not putting the
F
parameter betweenC
andD
?void foo(int A, int B, int C, int F = -1, int D = -1, int E = -1) {
//...
}so that you can use it like this :
int A, B, C, D, E, F;
foo(A, B, C); // A B C -1 -1 -1
foo(A, B, C, F); // A B C -1 -1 F
foo(A, B, C, F, D); // A B C D -1 F
foo(A, B, C, F, D, E); // A B C D E F
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
But I also sometimes want to call the function without the F parameter (and with the D and E parameters)... :(
so write several overloads of the function...!
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
Hi, I have a function that is declared as follows: void func(int A, int B, int C, int D=-1, int E=-1); arguments D and E are optional, and so if the function is called like this: func(A,B,C); then D and E recieve the value -1. What I want to do is add another optional argument, let's say int F that will also receive -1 as default, for example. The problem is that sometimes I want to call the function only with the parameters A,B,C and F. If I declare the function like this: void func(int A, int B, int C, int D=-1, int E=-1, F); will I be able to call the function in this manner: func(A,B,C,,,F); ?
Hi, the simples solution is to provide a wrapper macro for your function
void func(int A, int B, int C, int D=-1, int E=-1, int F=-1); #define FUNC(a_, b_, c_, f_) func(a_, b_, c_, -1, -1, f_)
This way you can simply call it through the macro in those rare occasions where only the F should be given.
void Call() { int A, B, C, D, E, F; // normal call func(A, B, C); func(A, B, C, D, E, F); // special call FUNC(A, B, C, F); }
codito ergo sum
-
SWDevil wrote:
but if I declare it this way, will I be able to call the func without specifying the parameters D and E?
Well if you are desperate to get it working take a look at
va_arg
,va_list
,va_start
,va_end
. It's an option.
Owner drawn Jesus Loves
Owner drawn wrote:
Well if you are desperate to get it working take a look at va_arg, va_list, va_start, va_end. It's an option.
Well Back after long time:)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
But I also sometimes want to call the function without the F parameter (and with the D and E parameters)... :(
SWDevil wrote:
ut I also sometimes want to call the function without the F parameter (and with the D and E parameters)..
What about using Variable Argument System!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
SWDevil wrote:
but if I declare it this way, will I be able to call the func without specifying the parameters D and E?
Well if you are desperate to get it working take a look at
va_arg
,va_list
,va_start
,va_end
. It's an option.
Owner drawn Jesus Loves
Where are you buddy? These days you are not posting messages to the board.
Nobody can give you wiser advice than yourself. - Cicero
-
Where are you buddy? These days you are not posting messages to the board.
Nobody can give you wiser advice than yourself. - Cicero
brahmma wrote:
Where are you buddy? These days you are not posting messages to the board.
Hmm.. I was away for quite a while but now alive and kicking. ;)
Owner drawn Jesus Loves