初始化列表和赋值重载(运算符=)
赋值运算符的重载是否传播到初始值设定项列表?
例如,假设一个类:
class MyClass {
private:
std::string m_myString; //std::string overloads operator =
public:
MyClass(std::string myString);
}
和一个构造函数:
MyClass::MyClass(std::string myString)
: m_myString(myString)
{
}
初始化列表会计算出 std::string 上的赋值运算符重载吗?如果没有,有解决方法吗?
特别是对于海湾合作委员会。
Does the overloading of the assignment operator propagate to an initializer list?
For example, suppose a class:
class MyClass {
private:
std::string m_myString; //std::string overloads operator =
public:
MyClass(std::string myString);
}
And a constructor:
MyClass::MyClass(std::string myString)
: m_myString(myString)
{
}
Will the initializer list work out the assignment operator overload on std::string
? And if not, is there a workaround?
Particularly for GCC.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您缺少的是
赋值
和初始化
之间的区别。让我们看一个基本类型的简单例子:
上面的例子很简单,也不难理解。然而,当您进入用户定义类型时,事情就没那么简单了,因为对象是构造的。
例如,让我们看一下
std::string
这里的关键是
operator=
在不同的上下文中意味着不同的东西。这完全取决于左侧的内容。初始化列表仅进行初始化(因此称为初始化列表)。
请注意,当您在构造函数主体中调用
operator=
时,您现在正在进行赋值而不是初始化。I think what you are missing is the difference between
assignment
andinitialization
.Lets look at a simple example with a fundamental type:
The above example is simple and not difficult to understand. However, when you get into user-defined types, it is not as simple because objects are constructed.
For example, lets look at
std::string
The key thing here is
operator=
means different things in different contexts. It all depends on what is on the left hand side.And initializer lists do initialization only (hence the name initializer list).
Just be aware, when you call
operator=
in the body of the constructor, you are now doing assignment and not initialization.我相信它将使用复制构造函数而不是赋值运算符。
I believe it will use the copy constructor rather than the assignment operator.
请注意,这里有两份副本:一份用于初始化参数
myString
,一份用于初始化成员m_myString
。你不想要这样。在 C++03 中,您将通过 const 引用获取参数:而在 C++11 中,您将通过值获取参数,然后手动将其移动到成员中:
Note that you have two copies here: one to initialize the parameter
myString
, and one to initialize the memberm_myString
. You don't want that. In C++03, you would take the parameter by const reference:And in C++11, you would take the parameter by value and then manually move it into the member: