Initialization of a variable in the header of a C ++ function

I came across some C ++ code that looks like this (simplified for this post):

(Here the function prototype is in someCode.hpp )

 void someFunction(const double & a, double & b, const double c = 0, const double * d = 0); 

(Here is the first line of the function body located in someCode.cpp , which is #include someCode.hpp )

 void someFunction(const double & a, double & b, const double c, const double * d); 

Can I legally call someFunction using:

 someFunction(*ptr1, *ptr2); 

and / or

 someFunction(*ptr1, *ptr2, val1, &val2); 

where are the variables ptr1 , ptr2 , val and val2 defined accordingly, and val1 and val2 not equal to zero? Why or why not?

And if this is legal, is this syntax preferable to overloading a function to account for additional parameters?

+4
source share
4 answers

Yes, this is legal; this is called the default argument . I would say that he prefers overloading due to less code, yes.

As for your comment on const , this does not apply to the default value itself, it applies to the argument. If you have an argument like const char* fruit = "apple" , this does not mean that it should be called with a pointer to a character whose value matches the address of the string literal "apple" (which is good, since it would be difficult to guarantee) . It just means that it should be called with a pointer to constant characters and tells you that the called function should not be written to this memory, it is read only from.

+5
source

Yes, the parameters are optional, and when you do not pass them, the specified default values โ€‹โ€‹will be used.

It has some advantages and disadvantages for using default parameter values โ€‹โ€‹instead of overloading. The advantage is that both the interface and part of the implementation are typed. But the disadvantage is that the default value is part of the interface with all its consequences. Then, when you change the default value, you, for example, have to recompile a lot of code instead of a single file when overloaded.

I personally prefer the default options.

+1
source

I would like to talk a bit about the preference of the default options for overloading.

Usually they relate to all the reasons mentioned in other answers, primarily to a smaller template.

There are also good reasons that in some situations overwhelm a better alternative:

Default values โ€‹โ€‹are part of the interface, changes can break clients (as @Juraj already noted). In addition, Overloads makes it easy to add additional (combinations) of parameters without breaking the (binary) interface.

Overloads are allowed during compilation, which makes it possible to optimize the optimization (esp inlining) of the compiler. for example if you have something like this:

 void foo(Something* param = 0) { if (param == 0) { simpleAlgorithm(); } else { complexAlgorithm(param); } } 

Better use overload.

+1
source

Can I legally name someFunction using:

someFunction (* ptr1, * ptr2);

Absolutely! Yes, the other 2 variables that the function takes will have the default values โ€‹โ€‹that you set in the header file, which is zero for both arguments.

But if you provide the third and fourth arguments to the function, then these values โ€‹โ€‹will be considered instead of the default values.

0
source

All Articles