13.22 HasPtr类的拷贝构造函数和拷贝赋值运算符代码如下:
#include <iostream>
#include <string>
using namespace::std;
class HasPtr {
public:
HasPtr(const string &s = string()):
ps(new string(s)), i(0) {}
HasPtr(const HasPtr &hp):
ps(new string(*hp.ps)), i(hp.i) {}
HasPtr& operator=(const HasPtr&);
~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;
}
13.24 未定义析构函数,销毁HasPtr对象时,合成的析构函数不会释放ps指向的内存; 未定义拷贝构造函数,合成的拷贝构造函数直接复制ps成员,两个HasPtr指向相同的string,如果一个HasPtr的string改变,会导致另一个HasPtr的string也发生改变;如果一个HasPtr对象被销毁,则的另一个HasPtr的ps会成为空悬指针。
13.25 StrBlob类值版本的拷贝构造函数和拷贝赋值运算符,应该拷贝data指向的vector,而不是直接拷贝data;data类型是shared_ptr,能够正确管理资源的分配和释放,无需定义析构函数,销毁StrBlob对象自动调用shared_ptr的析构函数即可。
13.26 StrBlob类:
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class StrBlob {
public:
typedef vector<string>::size_type size_type;
StrBlob();
StrBlob(initializer_list<string> il);
StrBlob(const StrBlob&);
StrBlob &operator=(const StrBlob&);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const string &t) { data->push_back(t); }
void pop_back();
string& front();
string& back();
private:
shared_ptr<vector<string>> data;
void check(size_type i, const string &msg) const;
};
void StrBlob::check(size_type i, const string &msg) const
{
if(i >= data->size())
throw out_of_range(msg);
}
StrBlob::StrBlob():
data(make_shared<vector<string>>()) {}
StrBlob::StrBlob(initializer_list<string> i1):
data(make_shared<vector<string>>(i1)) {}
StrBlob::StrBlob(const StrBlob &s):
data(make_shared<vector<string>>(*s.data)) {}
StrBlob& StrBlob::operator=(const StrBlob &rhs)
{
data = make_shared<vector<string>>(*rhs.data);
return *this;
}
void StrBlob::pop_back()
{
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
string& StrBlob::front()
{
check(0, "front on empty StrBlob");
return data->front();
}
string& StrBlob::back()
{
check(0, "back on empty StrBlob");
return data->back();
}