C++ Primer(第5版) 练习 13.7
练习 13.7 当我们将一个StrBlob赋值给另一个StrBlob时,会发生什么?赋值StrBlobPtr呢?
环境:Linux Ubuntu(云服务器)
工具:vim
解释
class StrBlob{
friend class StrBlobPtr;
public:
typedef vector<string>::size_type size_type;
StrBlob();
StrBlob(initializer_list<string> il);
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();
string &front() const;
string &back() const;
StrBlobPtr begin();
StrBlobPtr end();
private:
shared_ptr<vector<string>> data;
void check(size_type i, const string &msg) const;
};
class StrBlobPtr{
public:
StrBlobPtr(): curr(0) {}
StrBlobPtr(StrBlob &s, size_t sz = 0): wptr(s.data), curr(sz) {}
string &deref() const;
StrBlobPtr &incr();
private:
shared_ptr<vector<string>> check(size_t, const string &) const;
weak_ptr<vector<string>> wptr;
size_t curr;
};
当对StrBlob赋值时,会调用成员data的拷贝赋值运算符将一个对象赋值给另一个。
当对StrBlobPtr赋值时,会调用wptr的拷贝赋值运算符,成员curr直接赋值。