标签:nbsp c++ ptr space span 独立 拷贝 3.2 info
• 类的行为像一个值。意味着它应该有自己的状态,当我们拷贝一个像值得对象时,副本和原对象是完全独立的,改变副本不会对原对象有任何影响。
• 行为像指针的类则共享状态。当我们拷贝一个这种类的对象时,副本和原对象使用相同的底层数据,改变副本也会改变原对象。
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class HasPtr { 6 public: 7 HasPtr(const string &s = string()) : ps(new string(s)), i(0) {} 8 HasPtr(const HasPtr &rhs) : ps(new string(*rhs.ps)), i(rhs.i){} 9 10 HasPtr &operator=(const HasPtr &rhs); 11 HasPtr &operator=(const string &rhs); 12 string &operator*(); 13 14 ~HasPtr() { delete ps; } 15 private: 16 string *ps; 17 int i; 18 }; 19 20 HasPtr &HasPtr::operator=(const HasPtr &rhs) 21 { 22 auto newp = new string(*rhs.ps); 23 delete ps; 24 ps = newp; 25 i = rhs.i; 26 return *this; 27 } 28 29 HasPtr &HasPtr::operator=(const string &rhs) 30 { 31 *ps = rhs; 32 return *this; 33 } 34 35 string& HasPtr::operator*() 36 { 37 return *ps; 38 } 39 40 int main() 41 { 42 HasPtr h("hi mom!"); 43 HasPtr h2(h); 44 HasPtr h3(h); 45 h2 = "hi dad!"; 46 h3 = "hi, son!"; 47 cout << "h: " << *h << endl; 48 cout << "h2: " << *h2 << endl; 49 cout << "h3: " << *h3 << endl; 50 return 0; 51 }
运行结果:
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class HasPtr { 6 public: 7 HasPtr(const string &s = string()): ps(new string(s)), i(0) {} 8 HasPtr(const HasPtr &rhs): ps(new string(*rhs.ps)), i(rhs.i) {} 9 10 HasPtr& operator=(const HasPtr &rhs); 11 ~HasPtr() { delete ps; } 12 private: 13 string *ps; 14 int i; 15 }; 16 17 HasPtr& HasPtr::operator=(const HasPtr &rhs) 18 { 19 auto newp = new string(*rhs.ps); 20 delete ps; 21 ps = newp; 22 return *this; 23 }
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 class HasPtr { 6 public: 7 HasPtr(const string &s = string()): ps(new string(s)), i(0), use(new size_t(1)) {} 8 HasPtr(const HasPtr &rhs): ps(rhs.ps), i(rhs.i), use(rhs.use) { ++*use; } 9 10 HasPtr &operator=(const HasPtr &rhs); 11 ~HasPtr(); 12 13 private: 14 string * ps; 15 int i; 16 size_t *use; //用来记录有多少个对象共享*ps的成员 17 }; 18 19 HasPtr &HasPtr::operator=(const HasPtr &rhs) 20 { 21 ++*rhs.use; 22 if (--*use == 0) 23 { 24 delete ps; 25 delete use; 26 } 27 ps = rhs.ps; 28 i = rhs.i; 29 use = rhs.use; 30 return *this; 31 } 32 33 HasPtr::~HasPtr() 34 { 35 if (--*use == 0) 36 { 37 delete ps; 38 delete use; 39 } 40 }
标签:nbsp c++ ptr space span 独立 拷贝 3.2 info
原文地址:https://www.cnblogs.com/sunbines/p/9004545.html