c++ primer(第五版)笔记 第十三章(2) 拷贝控制

31 篇文章 0 订阅
#include<string>
#include<iostream>

// 行为像值的类
class HasPtr{
public:
	friend void swap( HasPtr& lhp, HasPtr& rhp);
	
	HasPtr( const std::string &s = std::string()):
		ps( new std::string( s)), num( 0) {
		std::cout << "constructor" << std::endl;
	}
	HasPtr( const HasPtr& hp):
		ps( new std::string( *( hp.ps))), num( hp.num) {
			std::cout << "copy constructor" << std::endl;
	}
	// 类值拷贝赋值运算符,通常组合了析构和构造函数的操作
	// 如果将一个对象赋予它自身,赋值运算符必须能正确工作
	// HasPtr& operator=( const HasPtr& hp){
		// num = hp.num;
		// auto newp = new std::string( *( hp.ps));
		// delete ps;
		// ps = newp;	// ps = new std::string( *( hp.ps)); 错误写法,如果 hp 和 *this是同一个对象,上面的 delete 释放了指向的内存
		// std::cout << "operator=" << std::endl;
		// return *this;
	// }
	// //使用 swap 的赋值运算符,多一次拷贝构造和析构,但可以有效防止自赋值异常
	HasPtr& operator=( HasPtr hp){
		std::cout << "swap operator=" << std::endl;
		swap( *this, hp);
		return *this;
	}
	~HasPtr(){
		std::cout << "destructor" << std::endl;
		delete ps;
	}
	
private:
	int num;
	std::string *ps;
};

// 写法一:std::swap()
// 写法二:using std::swap
// 第一种指定使用标准库算法,第二种写法会在类型自带的算法与标准库的算法进行函数匹配
inline void swap( HasPtr& lhp, HasPtr& rhp){
	std::swap( lhp.ps, rhp.ps);
	std::swap( lhp.num, rhp.num);
	std::cout << "HasPtr friend swap" << std::endl;
}


// // 行为像指针的类
// class HasPtr{
// public:
	// HasPtr( const std::string &s = std::string()):
		// ps( new std::string( s)), num( 0), cnt( new std::size_t( 1)) {}
		
	// HasPtr( const HasPtr& p):
		// ps( p.ps), num( p.num), cnt( p.cnt) { ++*cnt;}
		
	// HasPtr& operator=( const HasPtr& hp){
		// ++*hp.cnt;
		// if( --*cnt == 0){
			// delete ps;
			// delete cnt;
		// }
		// ps = hp.ps;
		// num = hp.num;
		// cnt = hp.cnt;
		// return *this;
	// }
	
	// ~HasPtr(){
		// if( --*cnt == 0){
			// delete ps;
			// delete cnt;
		// }
	// }
	
// private:
	// std::string *ps;
	// int num;
	// std::size_t *cnt;
// };

int main(){
	HasPtr hp("hello");
	std::cout << "******************" << std::endl;
	HasPtr hp2(hp);
	std::cout << "******************" << std::endl;
	HasPtr hp3;
	std::cout << "******************" << std::endl;
	hp3 = hp;
	std::cout << "******************" << std::endl;
	using std::swap;
	swap(hp, hp3);
	std::cout << "******************" << std::endl;
	// 使用 swap 版 operator 的输出:
		// constructor
		// ******************
		// copy constructor
		// ******************
		// constructor
		// ******************
		// copy constructor
		// swap operator=
		// HasPtr friend swap
		// destructor
		// ******************
		// HasPtr friend swap
		// ******************
		// destructor
		// destructor
		// destructor
		
	//使用引用参数的operator=输出:
		// constructor
		// ******************
		// copy constructor
		// ******************
		// constructor
		// ******************
		// operator=
		// ******************
		// HasPtr friend swap
		// ******************
		// destructor
		// destructor
		// destructor

	return 1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值