Initializing variable in C++ function header

13,598

Solution 1

Yes, this is legal, this is called default arguments. I would say it's preferred to overloading due to involving less code, yes.

Regarding your comment about const, that doesn't apply to the default value itself, it applies to the argument. If you have an argument of type const char* fruit = "apple", that doesn't mean it has to be called with a character pointer whose value is the same as the address of the "apple" string literal (which is good, since that would be hard to guarantee). It just means that it has to be called with a pointer to constant characters, and tells you that the function being called doesn't need to write to that memory, it is only read from.

Solution 2

Yes, the parameters are optional and when you don't pass them, the given default values will be used.

It has some advantages and disadvantages to use default parameter values instead of overloading. The advantage is less typing in both interface and implementation part. But the disadvantage is that the default value is a part of interface with all its consequences. Then when you change the default value, you for example need to recompile a lot of code instead of a single file when using overloading.

I personally prefer default parameters.

Solution 3

I'd like to expand a bit on whether Default Parameters are preferred over overloading.

Usually they are for all the reasons given in the other answers, most notably less boilerplate code.

There are also valid reasons that make overloading a better alternative in some situations:

Default values are part of the interface, changes might break clients (as @Juraj already noted) Additionally Overloads make it easier to add additional (combinations of) parameters, without breaking the (binary) interface.

Overloads are resolved at compile time, which can give the compiler better optimization (esp inlining) possibilities. e.g. if you have something like this:

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

It might be better to use overloads.

Share:
13,598
Evan
Author by

Evan

Updated on June 17, 2022

Comments

  • Evan
    Evan about 2 years

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

    (Here's the function prototype located in someCode.hpp)

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

    (Here's the first line of the function body located in someCode.cpp that #include's 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 the variables ptr1, ptr2, val, and val2 have been defined appropriately and val1 and val2 do not equal zero? Why or why not?

    And if it is legal, is this syntax preferred vs overloading a function to account for the optional parameters?

  • Evan
    Evan about 13 years
    Thanks for the reply and for the link answered. I didn't know what these were called. Why is it legal if the default arguments are const? It seems like making them const means they can't be changed.
  • Omnifarious
    Omnifarious about 13 years
    @Evan: The const qualifier applies to the function argument, not the default value that's supplied. The assignment of the default value happens at function call time, just like the assignment of a supplied argument. If things worked the way you were thinking, then it would be illegal to call a function with const parameters more than once.