How to omit parameters in the actual parameter passage?

Asked

Viewed 1,038 times

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?

2 answers

5


The only thing that is wrong in your attempt is that the value default need to be in the statement and not in the implementation. So:

minhaFuncao(int, int, float = 0); //declaração

minhaFuncao(int x, int y, float a) {} //definição/implementação

I put in the Github for future reference.

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

You cannot skip one and put another, when you have an optional parameter, all of the following must be optional.

  • 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?

  • Yes, it’s all right.

  • Thanks, good that you have how to do this, I really needed, will spare me some lines.

  • 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 statement minhaFuncao(int x, int y, float a = 0, int b=0, int c=0); and wanted to omit the parameter "int b" only?

  • This is not possible, as it is in your question.

  • 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.

Show 1 more comment

3

The value default a parameter must appear in the function statement, since the statement is the only thing the caller sees.

For example, if you put the statement in a header file, and the setting in a file .cpp separate, and #include (include) the header of a file .cpp different, you will be able to see more easily the difference.

Consider:

minhalib. h

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

minhalib.cpp

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

cpp testing.

#include "minhalib.h"

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

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

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

minhalib. h

void minhaFuncao(int x, int y, float a = 0);
  • I appreciate the explanation, I was in that situation anyway, separate files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.