类值类:
//行为像值的
class A
{
public:
A() = default;
A(string s) : ps(new string(s)), i(0) {}
A(const A& a) :ps(new string(*a.ps)), i(a.i) {} //每个类对象有自己的一份资源拷贝
A& operator=(const A& rhs)
{
string* newp = new string(*rhs.ps);//释放左侧资源前先拷贝右侧对象资源,因为有可能指向同一份资源,即自赋值
delete ps;
ps = newp;
i = rhs.i;
return *this;
}
~A() { delete ps; }
private:
string* ps;
int i;
};
//一种优化拷贝赋值运算符的方法:copy and swap
class A
{
friend void swap(const A&lhs, const A& rhs);
public:
A() = default;
A(string s) : ps(new string(s)), i(0) {}
A(const A& a) :ps(new string(*a.ps)), i(a.i) {} //每个类对象有自己的一份资源拷贝
A& operator=(A rhs) //值传递参数,调用拷贝构造函数,得到原右侧对象的一份拷贝
{
swap(*this, rhs); //调用自定义swap操作而非标准库版本,swap调用应该不加限定,由编译器匹配
return *this;
} //运算符调用结束,析构函数执行,销毁rhs,此时rhs指向原来左侧对象内存
~A() { delete ps; }
private:
string* ps;
int i;
};
inline
void swap(const A& lhs, const A& rhs)
{
using std::swap;
swap(lhs.ps, rhs.ps);
swap(lhs.i, rhs.i);
}
类指针类:
//行为像指针的类,对象共享底层资源,一种简单实现:智能指针
class A
{
public:
A() = default;
A(string s) : ps(new string(s)), i(0) {}
A(const A& a) :ps(a.ps), i(a.i) {} //拷贝指针
A& operator=(const A& rhs)
{
ps = rhs.ps; //智能指针自己管理引用计数、销毁与否等问题
i = rhs.i;
return *this;
}
~A() {}
private:
shared_ptr<string> ps;
int i;
};
//行为像指针的类,自己实现引用计数
class A
{
public:
A() = default;
A(string s) : ps(new string(s)), i(0),use(new size_t(1)) {} //初始化计数器为1
A(const A& a) :ps(a.ps), i(a.i), use(a.use) { ++*use; }//调用拷贝构造函数会递增引用计数
A& operator=(const A& rhs)
{
++*rhs.use; //先递增右侧被拷贝对象计数器
if (!(--*use)) //递减左侧被赋值对象计数器,如果为0释放资源
{
delete ps;
delete use;
}
ps = rhs.ps;
i = rhs.i;
use = rhs.use;
return *this;
}
~A() //析构函数先递减计数器,任何时候计数器为0就释放内存
{
if (!(--*use))
{
delete ps;
delete use;
}
}
private:
string* ps;
int i;
size_t* use;//指向计数器的指针,也要动态分配
};