C++ 拷贝构造函数和重载赋值操作符相互调用分析

结论:

从面相对象编程的角度考虑,拷贝构造函数调用重载赋值操作符,重载赋值操作符调用拷贝构造函数的写法都是没有意义的。应该避免。

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.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值