习题13.13
class Employee {
static int sn;
string name;
int mysn;
public:
Employee() { mysn = sn++; }
Employee(const string &n) :name(n) { mysn = sn++; }
Employee(const Employee&rhs) { name = rhs.name; mysn = rhs.mysn; }//mysn = sn++; }
Employee& operator = (const Employee& rhs) { name = rhs.name; return *this; }
const string& get_name() { return name; }
const int& get_mysn() { return mysn; }
};
int Employee::sn = 0;
class numbered {
private:
static int seq ;
public:
numbered() { mysn = seq++; };
numbered(const numbered&) { mysn = seq++; }
int mysn;
};
void f(Employee &s)
{
cout << s.get_name() << ":" << s.get_mysn() << endl;
}
int main()
{
Employee a("高"), b = a, c;// b = a,拷贝初始化,拷贝构造函数
c = b;//赋值操作符
f(a);
f(b);
f(c);
}
习题13.22
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(const HasPtr &hp) { ps = new string(*hp.ps); i = hp.i; }
HasPtr& operator = (const HasPtr &rhs) { auto newps = new string(*rhs.ps); delete ps; ps = newps; i = rhs.i; return *this; }
HasPtr& operator = (const string &s) { *ps = s; return *this; }
string& operator*() { return *ps; }
~HasPtr() { delete ps; }
private:
string *ps;
int i;
};
int main()
{
HasPtr h("hi man!");
HasPtr h2(h);
HasPtr h3 = h;z
h2 = "hi doode!";
h3 = "hi boy!";
cout << "h: " << *h << endl;
cout << "h2: " << *h2 << endl;
cout << "h3: " << *h3 << endl;
return 0;
}
习题13.26
#include"myStrBlob.h"
int main()
{
StrBlob b1;
{
StrBlob b2 = { "a","an","the" };
b1 = b2;
b2.push_back("about");
cout << "b2大小为" << b2.size() << endl;
cout << "b2首尾元素为" << b2.front() << " " << b2.back() << endl;
}
cout << "b1大小为" << b1.size() << endl;
cout << "b1首尾元素为" << b1.front() << " " << b1.back() << endl;
StrBlob b3 = b1;
b3.push_back("next");
cout << "b3大小为" << b3.size() << endl;
cout << "b3首尾元素为" << b3.front() << " " << b3.back() << endl;
cout << "b1全部元素: " << endl;
for (auto i = b1.begin(); neq(i, b1.end()); i.incr())
cout << i.deref() << endl ;
}
习题13.27
class HasPtr {
friend void swap(HasPtr &lhs, HasPtr &rhs);
public:
HasPtr(const string &s = string()) :ps(new string(s)), i(0), use(new size_t(1)) {}
HasPtr(const HasPtr &p) : ps(p.ps), i(p.i), use(p.use) { ++*use; }
HasPtr& operator = (const HasPtr &p);
HasPtr& operator = (const string &s);
string& operator *();//解引用
bool HasPtr::operator<(const HasPtr&rhs) const;
~HasPtr();
private:
string *ps;
int i;
size_t *use;//引用计数
};
HasPtr::~HasPtr()
{
if (--*use == 0) {
delete use;
delete ps;
}
}
HasPtr& HasPtr::operator=(const HasPtr &p)
{
if (--*use == 0) {
delete ps;
delete use;
}
++*p.use;
use = p.use;
i = p.i;
ps = p.ps;
return *this;
}
HasPtr& HasPtr::operator=(const string &s)
{
*ps = s;
return *this;
}
string& HasPtr::operator*()
{
return *ps;
}
inline void swap(HasPtr&lhs, HasPtr &rhs)
{
cout << "交换" << *rhs.ps << "和" << *lhs.ps << endl;
swap(lhs.ps, rhs.ps);
swap(lhs.i, rhs.i);
}
bool HasPtr::operator<(const HasPtr&rhs) const
{
return *ps < *rhs.ps;
}
int main()
{
vector<HasPtr> vh;
int n = 100;
for (int i = 0; i < n; i++)
vh.push_back(to_string(n - i));
for (auto p : vh)
cout << *p << " ";
cout << endl;
sort(vh.begin(), vh.end());
for (auto p : vh)
cout << *p << " ";
cout << endl;
return 0;
}