How to omit parameters in the actual parameter pass?

2

I have a C ++ function called minhaFuncao() declared like this:

minhaFuncao(int x, int y, float a);

and implemented like this:

minhaFuncao(int x, int y, float a=0){
...
}

What I'm trying to say is, when I call minhaFuncao and pass the actual parameters to it, I wanted to be able to omit some parameters, that is, if I do not pass the actual parameter, the formal parameter receives the value that already it was assigned as float a=0 , I think of something like this:

minhaFuncao(1, 1);

Notice that I did not pass the actual parameter that would be assigned to float a , so what I wanted was for it not to be passed to receive "0" or any other default value previously written to the function implementation. But if I decide to pass, that it normally receives, as below:

minhaFUncao(1, 1, 1.5);

Now realize that I passed the actual parameter "1.5" to float a , I needed it to be "1.5" now instead of "0" set as the default.

There is some way to do this in C ++, I remember I already read it, but I'm not finding it at all, I do not know what words to use to search.

One thing I noticed 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, like this:

minhaFuncao(int x, int y, float a, int b=0, int c=0){
...
}

Because at the time of calling minhaFuncao() , in the first form, it would go wrong, it would not know what is being omitted or not, so that would have to have this rule, or a way to specify all parameters at the time of passing the real ones like this:

minhaFuncao(x=1, y=1, b=1, c=1);

here I omitted the float a parameter. What's the right way to do this in C ++ if it's possible?

    
asked by anonymous 28.06.2017 / 20:25

2 answers

4

The only thing wrong with your attempt is that the default value must be in the statement, not the implementation. So:

minhaFuncao(int, int, float = 0);

minhaFuncao(int x, int y, float a) {}

The C ++ compiler will assign you the value before calling the function. This is the purest syntactic sugar.

You can not skip one and put another, when you have an optional parameter, all of the following should be optional.

    
28.06.2017 / 20:32
3
The default value of a parameter should appear in the function declaration, since the declaration is the only thing the caller sees.

For example, if you put the statement in a header file, and the definition in a .cpp file, and #include (include) the header of a .cpp file, you can see more ease the difference.

Consider:

minhalib.h

void minhaFuncao(int x, int y, float a);

minhalib.cpp

void minhaFuncao(int x, int y, float a = 0) {
   ...
}

test.cpp

#include "minhalib.h"

int main() {
    minhaFuncao(1, 2);
}

The compilation of teste.cpp will not see the declaration of the default parameter, failing with an error.

For this reason, the default parameter definition is usually specified in the function declaration.

minhalib.h

void minhaFuncao(int x, int y, float a = 0);
    
28.06.2017 / 21:02