//我们为什么需要提供copy&swap这样的方式进行编码呢
//在c++中异常安全分为三个安全等级
//1基本保证:如果异常被抛出,对象内的任何成员仍能保持有效状态,没有数据的破坏及资源泄露
//2强烈保证:如果异常被抛出,对象的状态保持不变,如果调用成功,则完全成功,如果调用失败,对象依旧是调用前的状态.
//3不抛异常保证:函数不会抛出任何异常.
//
//如果一个函数不具有上述的保证之一,那么就不具备异常安全。copy&swap就是能保证异常安全的利器。
//
//
//假设有一个类需要管理一个内存
//
//
//
#include <iostream>
#include <algorithm>
//#include <memory.h>
class dumb_arry{
private:
int* arr;
int size;
public:
dumb_arry(int num);//构造函数
dumb_arry(const dumb_arry& other);//拷贝构造
//dumb_arry& operator=(const dumb_arry& other);//拷贝赋值
dumb_arry& operator=(dumb_arry other);//拷贝赋值
//void operator=(const dumb_arry& other);//拷贝赋值
void swap(dumb_arry other);
~dumb_arry();
public:
void print();
};
//成员的初始化顺序并不是按照初始化列表顺序,而是按照类中的顺序进行的,本例就是会先初始化arr
dumb_arry::dumb_arry(int num):size(num),arr(num? new int[size] : NULL)
{
/*
if(arr == NULL){
std::cout<<"没有分配内存"<<std::endl;
exit(0);
}
for(int i =0;i<size;i++){
arr[i] = i;
}
std::cout<<"构造成功"<<std::endl;
*/
}
//要理解拷贝构造是一个构造的过程
dumb_arry::dumb_arry(const dumb_arry& other)
{
size = other.size;
if(size){
arr = new int[size];
std::copy(other.arr,other.arr + other.size,arr);
//memcpy(arr,other.arr,other.size * sizeof(int));
}
}
dumb_arry::~dumb_arry()
{
delete[] arr;
}
/*
//1 提供强烈的异常保证
//虽然是提供了强烈的保证但是代码冗余
dumb_arry& dumb_arry::operator=(const dumb_arry& other)
{
if(this != &other){
int* temArr = new int[other.size];
std::copy(other.arr,other.arr + other.size,temArr);
//memcpy(temArr,other.arr,other.size *sizeof(int));
delete[] arr;
size = other.size;
arr = temArr;
}
return *this;
}
*/
//一种个好的实现方式
void dumb_arry::swap(dumb_arry other)
{
std::swap(arr,other.arr);
std::swap(size,other.size);
}
//改进版的opreator
dumb_arry& dumb_arry::operator=(dumb_arry other)
{
swap(other); //按值传递 other的内容不会改变,编译器会调用拷贝构造函数,所以交换后的指针和other的指针不同
return *this;
}
/*
//2 提供了基本的保证
dumb_arry& dumb_arry::operator=(const dumb_arry& other)
{
if(this != &other){
delete[] arr;
arr = new int[other.size]; //如果new失败,那么this就是被修改了,数组数据丢失
std::copy(other.arr,other.arr + other.size,arr);
size = other.size;
}
return *this;
}
*/
/*
//返回引用的目的是连等的时候前面的表达式需要返回一个对象,作为后一个等号的参数
void dumb_arry::operator=(const dumb_arry& other)
{
if(this != &other){
delete[] arr;
arr = new int[other.size]; //如果new失败,那么this就是被修改了,数组数据丢失
std::copy(other.arr,other.arr + other.size,arr);
size = other.size;
}
}
*/
void dumb_arry::print()
{
std::cout<< size <<std::endl;
}
/********************************************
//copy & swap 作用在异常安全的代码
//
//比如
void DoSomething(Example& e){
Example temp(e);
temp.func1(); //may except;
temp.func2(); //may except;
...
swap(e, temp); //no except
} //如果func1, func2之类的抛异常, e的状态却没有改变, 仍然可用
*********************************************/
int main()
{
dumb_arry Arr(1);
dumb_arry arr(2);
arr = Arr;
arr.print();
Arr.print();
return 0;
}