default parameters in .h and .cpp files

做~自己de王妃 提交于 2019-11-30 04:20:41
David Brown

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.

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.

.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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!