3
I have a C++ function called minhaFuncao()
thus declared:
minhaFuncao(int x, int y, float a);
and thus implemented:
minhaFuncao(int x, int y, float a=0){
...
}
What I’m trying to say is, when you call minhaFuncao
and pass the real parameters to her, I wanted to be able to omit some parameters, ie, if I do not pass the real parameter, the formal parameter receives the value that has already been assigned to it as in float a=0
, think of something like that:
minhaFuncao(1, 1);
realize that I did not pass the actual parameter that would be assigned to float a
, So what I wanted was for him not to be passed to receive "0" or any other default value previously written in the function implementation. But should I decide to pass, let him receive normally, as below:
minhaFUncao(1, 1, 1.5);
realize now that I passed the real parameter "1.5" to float a
, needed it to be worth "1.5" now instead of the "0" set as standard.
There is some way to do this in C++, I remember I’ve read this, but I’m not finding it at all, I don’t know what words to use to search.
One thing I realized, is that if there is a way to do this, it will have its limitations, as the function implemented below would not be possible:
minhaFuncao(int x, int y, float a=0, int b, int c=0){
...
}
I think one of the rules for this would be that only the final parameters could be omitted, so:
minhaFuncao(int x, int y, float a, int b=0, int c=0){
...
}
because when it’s time to call minhaFuncao()
, in the first way, would go wrong, would not know what is being omitted or not, so would have to have this rule, or a way to specify all parameters when passing the real ones like this:
minhaFuncao(x=1, y=1, b=1, c=1);
here I have omitted the parameter float a
. What the right way to do this in c++ if possible?
So in the statement I put like this:
minhaFuncao(int x, int y, float a = 0);
and in the implementation thus:minhaFuncao(int x, int y, float a);
? And the second part of the question, is it correct? can I specify the parameters in the function call?– PerduGames
Yes, it’s all right.
– Maniero
Thanks, good that you have how to do this, I really needed, will spare me some lines.
– PerduGames
I tested here now, it does not work out the specify part, I get an error if I do this:
minhaFuncao(x=1, y=1, b=1, c=1);
, how would I do if I had such a statementminhaFuncao(int x, int y, float a = 0, int b=0, int c=0);
and wanted to omit the parameter "int b" only?– PerduGames
This is not possible, as it is in your question.
– Maniero
There is an assumption that could make this way, I thought that even the assumption was worth kkkkk It would be good, but already giving to omit the parameters in order, already help.
– PerduGames