C++ .h 和 .cpp 文件中的默认参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14902129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
default parameters in .h and .cpp files
提问by sudeepdino008
COMPILER:g++ 4.7.2
编译器: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:
好的。所以我对.h
和.cpp
文件中的默认参数感到困惑。很多地方(包括本站)都提到默认参数只能在.h文件中添加,而不能在.cpp文件中添加。然而,这段代码证明它是错误的:
test1.h
测试1.h
#pragma once
#include <iostream>
using namespace std;
class Class{
public:
Class(int, int, int=1);
};
test1.cpp
测试1.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:
现在,根据我的测试,可以将默认参数添加到.cpp
文件中。但是,以下限制仍然有效:
The default parameters present in
.cpp
and.h
file should not overlap. i.e.Class(a, b, c=1)
(in .h file) andClass::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 defpararule. Now,
The variables stated in the function declaration(
.h
file) should obey the defpararule i.e.Class(a, b=2, c)
(in .h file) is invalid irrespective of what's declared in .cpp file.If one considers the variables having default values (as an intersection of default values in
.h
and.cpp
files), it would follow the defpararule. i.e.Class(a, b, c=1)
(in .h file) andClass::Class(a,b=2,c)
( in.cpp
file) is valid. ButClass(a, b, c=1)
(in .h file) andClass::Class(a=2,b,c)
( in.cpp
file) is invalid.
.cpp
和.h
文件中存在的默认参数不应重叠。即Class(a, b, c=1)
(在 .h 文件中)和Class::Class(a,b,c=2)
(在 .cpp 文件中)无效。众所周知的规则是,一旦添加了默认参数,之后声明的所有变量也必须包含默认值。让我们称之为defpara规则。现在,
函数声明(
.h
文件)中声明的变量应遵守defpara规则,即Class(a, b=2, c)
(在 .h 文件中)无效,无论 .cpp 文件中声明的内容如何。如果考虑具有默认值的变量(作为
.h
和.cpp
文件中默认值的交集),它将遵循defpara规则。即Class(a, b, c=1)
(在 .h 文件中)和Class::Class(a,b=2,c)
(在.cpp
文件中)是有效的。但是Class(a, b, c=1)
(在 .h 文件中)和Class::Class(a=2,b,c)
(在.cpp
文件中)是无效的。
So....I am right, wrong???
所以......我是对的,错的???
采纳答案by 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.
这只有效,因为您的 main 函数也在您的test.cpp
文件中,因此它会看到您的类的实现中指定的默认参数。如果将main
函数放在仅包含 的单独文件中test.h
,则此代码将无法编译。
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.
另一种看待它的方式是,当其他一些包含时test.h
,所有代码看到的是在 中声明的内容test.h
,因此不会使用放在其他地方的默认参数。
回答by Mats Petersson
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.
由于编译器在编译调用函数的代码(在本例中为构造函数)时会添加默认参数,因此 .cpp 文件中的默认值无关紧要。
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].
当然,在这种情况下,头文件只有一个“用户”,并且只有一个调用构造函数的地方。但是在 .cpp 文件中使用默认值通常是错误的 [除非它是本地函数]。
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.
如果你“混合”默认值,你会得到非常“有趣”的错误——例如,如果你的 .cpp 有一个默认值,而头文件是不同的。如果你真的很熟练,你甚至可以让编译器为函数的不同调用生成不同的默认值,如果代码依赖于某个特定值的默认值,这几乎肯定会导致一些麻烦。并且不要试图将标头中的默认值复制到 .cpp 文件,“只是为了更容易查看”。如果有人更改了默认值,那么几乎可以肯定,这两个地方都不会更改,甚至可能更糟:更改错误的默认值,因此它不会执行预期的操作。
回答by Pete Becker
.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:
.h 与 .cpp 是一个红鲱鱼。规则是可以在函数声明和函数定义中使用默认参数。不允许重新定义默认参数,甚至不能重新定义相同的值。所以这是不合法的:
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:
在原始示例中,.h 文件定义了一个默认参数,任何使用该标头的翻译单元都可以使用该默认参数:
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:
此外,.cpp 文件定义了一个额外的默认参数,因此在该声明之后,可以使用一个、两个或三个参数调用构造函数:
Class c3(1, 2, 3);
class c2(1, 2);
class c1(1);
回答by Ed Heal
There is no such thing as a default parameter for the definition of a file in C++ - it only exists in the declaration.
C++ 中没有文件定义的默认参数这样的东西——它只存在于声明中。
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.
PS:Item 38/Scott Myers/Effective C++ - 永远不要重新定义继承的默认参数值。