C++ 显式复制构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11480545/
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
Explicit copy constructor
提问by abumusamq
I have extended std::string to fulfil my needs of having to write custom function build into string class called CustomString
我扩展了 std::string 以满足我必须将自定义函数构建写入名为CustomString 的字符串类的需要
I have defined constructors:
我已经定义了构造函数:
class CustomString : public std::string {
public:
explicit CustomString(void);
explicit CustomString(const std::string& str);
explicit CustomString(const CustomString& customString);
//assignment operator
CustomString& operator=(const CustomString& customString);
... };
In the third constructor (copy constructor) and assignment operator, whose definition is:
在第三个构造函数(复制构造函数)和赋值运算符中,其定义为:
CustomString::CustomString(const CustomString& customString):
std::string(static_cast<std::string>(customString))
{}
CustomString& CustomString::operator=(const CustomString& customString){
this->assign(static_cast<std::string>(customString));
return *this;
}
First since this is an "explicit"; meaning an explicit cast is needed to assign to another CustomString object; it's complaining about the assignment.
首先,因为这是一个“显式”;这意味着需要显式转换才能分配给另一个 CustomString 对象;它在抱怨这个任务。
CustomString s = CustomString("test");
I am not sure where exactly is casting needed explicitly.
我不确定在哪里明确需要强制转换。
The code works alright if copy constructor is not explicit but I would like to know and implement explicit definition instead of "guessing proper cast".
如果复制构造函数不是显式的,代码可以正常工作,但我想知道并实现显式定义,而不是“猜测正确的转换”。
回答by David Rodríguez - dribeas
The explicit copy constructor means that the copy constructor will not be called implicitly, which is what happens in the expression:
显式复制构造函数意味着不会隐式调用复制构造函数,这就是表达式中发生的情况:
CustomString s = CustomString("test");
This expression literally means: create a temporary CustomString
using the constructor that takes a const char*
. Implicitly call the copy constructor of CustomString
to copy from that temporary into s
.
这个表达式的字面意思是:CustomString
使用带有const char*
. 隐式调用 的复制构造函数CustomString
从那个临时复制到s
.
Now, if the code was correct (i.e. if the copy constructor was not explicit), the compiler would avoid the creation of the temporary and elide the copy by constructing s
directly with the string literal. But the compiler must still check that the construction can be done and fails there.
现在,如果代码是正确的(即,如果复制构造函数不是显式的),编译器将避免临时的创建并通过s
直接用字符串字面量构造来省略副本。但是编译器仍然必须检查构造是否可以完成并在那里失败。
You can call the copy constructor explicitly:
您可以显式调用复制构造函数:
CustomString s( CustomString("test") );
But I would recommend that you avoid the temporary altogether and just create s
with the const char*
:
不过,我建议你完全避免暂时的,只是创建s
与const char*
:
CustomString s( "test" );
Which is what the compiler would do anyway...
这就是编译器无论如何都会做的......
回答by ForEveR
Deriving from std::string is not safe, as std::string has no virtual destructor. As to your question - your copy constructors should not be explicit, to allow for such usage as:
从 std::string 派生是不安全的,因为 std::string 没有虚拟析构函数。至于你的问题 - 你的复制构造函数不应该是明确的,以允许这样的用法:
CustomString s = "test";
Also I have no idea why you would want to declare a copy-constructor as explicit, as it is not needed. An explicit copy-constructor will work only if you declare your CustomString object as:
此外,我不知道您为什么要将复制构造函数声明为显式,因为它不需要。只有将 CustomString 对象声明为:
CustomString s(CustomString("test"));