If I override operator=
will the copy constructor automatically use the new operator? Similarly, if I define a copy constructor, will operator=
automatically 'inherit' the behavior from the copy constructor?
如果我覆盖操作符=复制构造函数会自动使用新的操作符吗?类似地,如果我定义一个复制构造函数,那么操作符=会自动从复制构造函数“继承”行为吗?
5 个解决方案
#1
39
No, they are different operators.
不,它们是不同的运算符。
The copy constructor is for creating a new object. It copies a existing object to a newly constructed object.The copy constructor is used to initialize a new instance from an old instance. It is not necessarily called when passing variables by value into functions or as return values out of functions.
复制构造函数用于创建一个新对象。它将现有对象复制到新构造的对象。复制构造函数用于从一个旧实例初始化一个新实例。当将变量按值传递给函数或作为函数的返回值时,不一定要调用它。
The assignment operator is to deal with an already existing object. The assignment operator is used to change an existing instance to have the same values as the rvalue, which means that the instance has to be destroyed and re-initialized if it has internal dynamic memory.
赋值操作符是处理一个已经存在的对象。赋值操作符用于将现有实例更改为具有与rvalue相同的值,这意味着如果实例具有内部动态内存,则必须销毁并重新初始化该实例。
Useful link :
有用的链接:
- Copy Constructors, Assignment Operators, and More
- 复制构造函数、赋值操作符等等。
- Copy constructor and = operator overload in C++: is a common function possible?
- 在c++中,复制构造函数和=运算符重载:公共函数可能吗?
#2
11
No. Unless you define a copy ctor, a default will be generated (if needed). Unless you define an operator=, a default will be generated (if needed). They do not use each other, and you can change them independently.
不。除非定义复制ctor,否则将生成默认值(如果需要)。除非定义操作符=,否则将生成默认值(如果需要)。它们彼此不使用,您可以独立地更改它们。
#3
5
No. They are different objects.
不。它们是不同的对象。
If your concern is code duplication between copy constructor and assignment operator, consider the following idiom, named copy and swap :
如果您关心的是复制构造函数和赋值操作符之间的代码复制,请考虑下面的习惯用法,命名为copy和swap:
struct MyClass
{
MyClass(const MyClass&); // Implement copy logic here
void swap(MyClass&) throw(); // Implement a lightweight swap here (eg. swap pointers)
MyClass& operator=(MyClass x)
{
x.swap(*this);
return *this;
}
};
This way, the operator=
will use the copy constructor to build a new object, which will get exchanged with *this
and released (with the old this
inside) at function exit.
这样,操作符=将使用copy构造函数来构建一个新对象,该对象将与* This交换并在函数退出时释放(其中包含旧的This)。
#4
1
No, they are not the same operator.
不,它们不是同一个运算符。
#5
0
No.
不。
And definitely have a look at the rule of three (or rule of five when taking rvalues into account)
并且一定要看一下3的规则(或者考虑rvalues时5的规则)
#1
39
No, they are different operators.
不,它们是不同的运算符。
The copy constructor is for creating a new object. It copies a existing object to a newly constructed object.The copy constructor is used to initialize a new instance from an old instance. It is not necessarily called when passing variables by value into functions or as return values out of functions.
复制构造函数用于创建一个新对象。它将现有对象复制到新构造的对象。复制构造函数用于从一个旧实例初始化一个新实例。当将变量按值传递给函数或作为函数的返回值时,不一定要调用它。
The assignment operator is to deal with an already existing object. The assignment operator is used to change an existing instance to have the same values as the rvalue, which means that the instance has to be destroyed and re-initialized if it has internal dynamic memory.
赋值操作符是处理一个已经存在的对象。赋值操作符用于将现有实例更改为具有与rvalue相同的值,这意味着如果实例具有内部动态内存,则必须销毁并重新初始化该实例。
Useful link :
有用的链接:
- Copy Constructors, Assignment Operators, and More
- 复制构造函数、赋值操作符等等。
- Copy constructor and = operator overload in C++: is a common function possible?
- 在c++中,复制构造函数和=运算符重载:公共函数可能吗?
#2
11
No. Unless you define a copy ctor, a default will be generated (if needed). Unless you define an operator=, a default will be generated (if needed). They do not use each other, and you can change them independently.
不。除非定义复制ctor,否则将生成默认值(如果需要)。除非定义操作符=,否则将生成默认值(如果需要)。它们彼此不使用,您可以独立地更改它们。
#3
5
No. They are different objects.
不。它们是不同的对象。
If your concern is code duplication between copy constructor and assignment operator, consider the following idiom, named copy and swap :
如果您关心的是复制构造函数和赋值操作符之间的代码复制,请考虑下面的习惯用法,命名为copy和swap:
struct MyClass
{
MyClass(const MyClass&); // Implement copy logic here
void swap(MyClass&) throw(); // Implement a lightweight swap here (eg. swap pointers)
MyClass& operator=(MyClass x)
{
x.swap(*this);
return *this;
}
};
This way, the operator=
will use the copy constructor to build a new object, which will get exchanged with *this
and released (with the old this
inside) at function exit.
这样,操作符=将使用copy构造函数来构建一个新对象,该对象将与* This交换并在函数退出时释放(其中包含旧的This)。
#4
1
No, they are not the same operator.
不,它们不是同一个运算符。
#5
0
No.
不。
And definitely have a look at the rule of three (or rule of five when taking rvalues into account)
并且一定要看一下3的规则(或者考虑rvalues时5的规则)