拷贝控制和资源管理
• 类的行为像一个值。意味着它应该有自己的状态,当我们拷贝一个像值得对象时,副本和原对象是完全独立的,改变副本不会对原对象有任何影响。
• 行为像指针的类则共享状态。当我们拷贝一个这种类的对象时,副本和原对象使用相同的底层数据,改变副本也会改变原对象。
13.2节练习
#include<iostream>
#include<string>
using namespace std; class HasPtr {
public:
HasPtr(const string &s = string()): ps(new string(s)), i() {}
HasPtr(const HasPtr &rhs): ps(new string(*rhs.ps)), i(rhs.i) {} HasPtr &operator=(const HasPtr &rhs);
HasPtr &operator=(const string &rhs);
string &operator*(); ~HasPtr() { delete ps; }
private:
string *ps;
int i;
}; HasPtr &HasPtr::operator=(const HasPtr &rhs)
{
auto newp = new string(*rhs.ps);
delete ps;
ps = newp;
i = rhs.i;
return *this;
} HasPtr &HasPtr::operator=(const string &rhs)
{
*ps = rhs;
return *this;
} string& HasPtr::operator*()
{
return *ps;
} int main()
{
HasPtr h("hi mom!");
HasPtr h2(h);
HasPtr h3(h);
h2 = "hi dad!";
h3 = "hi, son!";
cout << "h: " << *h << endl;
cout << "h2: " << *h2 << endl;
cout << "h3: " << *h3 << endl;
return ;
}
运行结果:
行为像值的类
#include<iostream>
#include<string>
using namespace std; class HasPtr {
public:
HasPtr(const string &s = string()): ps(new string(s)), i() {}
HasPtr(const HasPtr &rhs): ps(new string(*rhs.ps)), i(rhs.i) {} HasPtr& operator=(const HasPtr &rhs);
~HasPtr() { delete ps; }
private:
string *ps;
int i;
}; HasPtr& HasPtr::operator=(const HasPtr &rhs)
{
auto newp = new string(*rhs.ps);
delete ps;
ps = newp;
return *this;
}
定义行为像指针的类
#include<iostream>
#include<string>
using namespace std; class HasPtr {
public:
HasPtr(const string &s = string()): ps(new string(s)), i(), use(new size_t()) {}
HasPtr(const HasPtr &rhs): ps(rhs.ps), i(rhs.i), use(rhs.use) { ++*use; } HasPtr &operator=(const HasPtr &rhs);
~HasPtr(); private:
string * ps;
int i;
size_t *use; //用来记录有多少个对象共享*ps的成员
}; HasPtr &HasPtr::operator=(const HasPtr &rhs)
{
++*rhs.use;
if (--*use == )
{
delete ps;
delete use;
}
ps = rhs.ps;
i = rhs.i;
use = rhs.use;
return *this;
} HasPtr::~HasPtr()
{
if (--*use == )
{
delete ps;
delete use;
}
}