default parameters in .h and .cpp files

2019-02-05 17:51发布

问题:

COMPILER: g++ 4.7.2

Ok. So I am confused about default parameters in .h and .cpp files. It is mentioned in many places( including this site) that default parameters can be added only in .h files and not in .cpp files. However, this code proves it wrong:

test1.h

#pragma once

#include <iostream>
using namespace std;

class Class{
public:
    Class(int, int, int=1);
};

test1.cpp

#include "test1.h"

Class::Class(int a, int b=2, int c)
{
    cout<<a<<" "<<b<<" "<<c<<endl;
}

int main()
{
    Class a(1);
    return 0;
}

Now, according to what I have tested, default parameters can be added to .cpp files. However, the following restrictions hold:

  1. The default parameters present in .cpp and .h file should not overlap. i.e. Class(a, b, c=1) (in .h file) and Class::Class(a,b,c=2)( in .cpp file) is invalid.

    It is a well known rule that once default parameters have been added, all the variables declared after that must also contain default values. Lets call this the defpara rule. Now,

  2. The variables stated in the function declaration( .h file) should obey the defpara rule i.e. Class(a, b=2, c) (in .h file) is invalid irrespective of what's declared in .cpp file.

  3. If one considers the variables having default values (as an intersection of default values in .h and .cpp files), it would follow the defpara rule. i.e. Class(a, b, c=1) (in .h file) and Class::Class(a,b=2,c)( in .cpp file) is valid. But Class(a, b, c=1) (in .h file) and Class::Class(a=2,b,c)( in .cpp file) is invalid.

So....I am right, wrong???

回答1:

This only works because your main function is also in your test.cpp file, so it sees the default argument specified in your class' implementation. If you put your main function in a separate file that only includes test.h, this code will not compile.

Another way of looking at it is, when some other includes test.h, all that code sees is what is declared in test.h, so default arguments put elsewhere will not be used.



回答2:

Defaults should always go in the header file, if the function is declared in a header file.

This is because the compiler will use the header file for ALL compile units that use your class [unless you are being "naughty" and don't use the header file everywhere it should go].

Since the compiler adds default arguments when it compiles the code that CALLS the function (in this case the constructor), it won't matter what the defaults are in the .cpp file.

Of course, in this case, there are only one "user" of the headerfile, and only one place where the constructor is called. But having defaults in the .cpp file is generally wrong [unless it's a local function].

You get very "interesting" bugs if you "mix" defaults - e.g. if your .cpp has one default, and the headefile a different one. If you are really skilled, you can even get the compiler to generate different defaults for different calls to the function, which will almost certainly lead to some headscratching if the code relies on the default being some particular value. And don't be tempted to copy the defaults from the header to the .cpp file "just to make it easier to see". If someone ever changes the default, then it's almost certainly either not going to change in both places, and possibly worse: change the wrong defaults, so it doesn't do what was intended.



回答3:

.h vs. .cpp is a red herring. The rule is that default arguments can be used in function declarations and in the function's definition. You are not allowed to redefine a default argument, not even to the same value. So this is not legal:

void f(int, int = 3);
void f(int, int = 3); // error: redefinition of default argument

However, subsequent declarations can add default arguments:

void f(int, int = 3);
void f(int = 4, int = 3);
f(); // calls f(4, 3);

Further, at any point where the function is called, the default arguments that have been seen at that point can be used:

void f(int, int =3);
f(1); // calls f(1, 3);
void f(int = 4, int = 3);
f(1); // calls f(1, 3);
f();  // calls f(4, 3);

In the original example, the .h file defines one default argument, and any translation unit that uses that header can use that default argument:

Class c3(1, 2, 3);
Class c2(1, 2);

Further, the .cpp file defines an additional default argument, so after that declaration the constructor can be called with one, two, or three arguments:

Class c3(1, 2, 3);
class c2(1, 2);
class c1(1);


回答4:

There is no such thing as a default parameter for the definition of a file in C++ - it only exists in the declaration.

What happens is the compiler sees a function that is missing the latter parameters. If those are default it can fill in the blanks to construct the object code to call the function as if the function call had those parameters.

PS: Item 38/Scott Myers/Effective C++ - Never redefine an inherited default parameter value.