从面相对象编程的角度考虑,拷贝构造函数调用重载赋值操作符,重载赋值操作符调用拷贝构造函数的写法都是没有意义的。应该避免。
Don't try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function that both call.
——Effective C++ Third Edition By Scott Meyers Item 12: Copy all parts of an object Things to Remember
首先明确:拷贝构造函数的存在意义是通过已有的对象构造新的对象,构造完毕后才有两个对象;重载赋值操作符的意义在于将一个对象的值赋给另一个对象,两个对象都已经构造完毕了。
拷贝构造函数调用重载赋值操作符:把已有对象的值赋给一个构造中的对象,虽然这个对象的内存已经分配好了。(可以接受,但是有可能导致循环调用重载赋值操作符和拷贝构造函数)
重载赋值操作符调用拷贝构造函数:把已有对象复制并赋值给这个对象。——多一个临时对象,而且导致循环调用重载赋值操作符。
例子1:拷贝构造函数调用重载赋值操作符(导致循环调用重载赋值操作符和拷贝构造函数 )
#include <iostream>
using namespace std;
class Base {
public:
Base() {cout << "Constructor invoked!" << endl;}
~Base() {cout << "Destructor invoked!" << endl;}
Base(const Base& rhs) {
cout << "Copy constructor invoked!" << endl;
operator=(rhs); // *this = rhs;
}
Base operator=(const Base& rhs) { // 问题出在这里,返回值不是引用会调用拷贝构造函数
cout << "Copy assignment operator invoked!" << endl;
return *this;
}
};
int main(int argc, char** argv) {
cout << "Hello World C++!" << endl;
Base a;
Base b(a); // Base b = Base(a);
return 0;
}
修改后
#include <iostream>
using namespace std;
class Base {
public:
Base() {cout << "Constructor invoked!" << endl;}
~Base() {cout << "Destructor invoked!" << endl;}
Base(const Base& rhs) {
cout << "Copy constructor invoked!" << endl;
operator=(rhs); // *this = rhs;
}
Base& operator=(const Base& rhs) { // 返回引用,可以接受
cout << "Copy assignment operator invoked!" << endl;
return *this;
}
};
int main(int argc, char** argv) {
cout << "Hello World C++!" << endl;
Base a;
Base b(a); // Base b = Base(a);
return 0;
}
这样做没有任何问题,但是破坏了拷贝构造函数的意义(不同人,可能理解不同),所以不推荐。
如果你认为拷贝构造函数体内就是需要这样赋值,Ok,这样做没有任何问题。请继续。
例子2:重载赋值操作符调用拷贝构造函数(导致循环调用重载赋值操作符 )
#include <iostream>
using namespace std;
class Base {
public:
Base() {cout << "Constructor invoked!" << endl;}
~Base() {cout << "Destructor invoked!" << endl;}
Base(const Base& rhs) {
cout << "Copy constructor invoked!" << endl;
}
Base& operator=(const Base& rhs) {
cout << "Copy assignment operator invoked!" << endl;
*this = Base(rhs);
return *this;
}
};
int main(int argc, char** argv) {
cout << "Hello World C++!" << endl;
Base a;
Base b(a); // Base b = Base(a);
b = a;
return 0;
}
还是那句话:
-
Don't try to implement one of the copying functions in terms of the other. Instead, put common functionality in a third function that both call.