国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

13.3

2019-11-11 05:32:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

13.29

swap(lhs.ps, rhs.ps);swap(lhs.i, rhs.i);

他們調(diào)用的是另一個(gè)版本的swap而不是 swap(Hasptr&, HasPtr&) 所以并不會(huì)遞歸

13.30

#include <string>#include <iostream>class HasPtr {public: friend void swap(HasPtr&, HasPtr&); HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { } HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { } HasPtr& Operator=(const HasPtr &hp) { auto new_p = new std::string(*hp.ps); delete ps; ps = new_p; i = hp.i; return *this; } ~HasPtr() { delete ps; } void show() { std::cout << *ps << std::endl; }PRivate: std::string *ps; int i;};inlinevoid swap(HasPtr& lhs, HasPtr& rhs){ using std::swap; swap(lhs.ps, rhs.ps); swap(lhs.i, rhs.i); std::cout << "call swap(HasPtr& lhs, HasPtr& rhs)" << std::endl;}

13.31

#include <string>#include <iostream>class HasPtr {public: friend void swap(HasPtr&, HasPtr&); friend bool operator<(const HasPtr &lhs, const HasPtr &rhs); HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { } HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { } HasPtr& operator=(HasPtr tmp) { this->swap(tmp); return *this; } ~HasPtr() { delete ps; } void swap(HasPtr &rhs) { using std::swap; swap(ps, rhs.ps); swap(i, rhs.i); std::cout << "call swap(HasPtr &rhs)" << std::endl; } void show() const { std::cout << *ps << std::endl; }private: std::string *ps; int i;};void swap(HasPtr& lhs, HasPtr& rhs){ lhs.swap(rhs);}bool operator<(const HasPtr &lhs, const HasPtr &rhs){ return *lhs.ps < *rhs.ps;}

13.32 不會(huì) 類(lèi)指針并沒(méi)有分配內(nèi)存的操作,所以并不會(huì)受益


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 紫阳县| 沐川县| 宝应县| 平邑县| 东安县| 鲜城| 靖远县| 内江市| 衢州市| 当阳市| 阿拉尔市| 辽宁省| 积石山| 留坝县| 泰宁县| 屏东市| 申扎县| 诏安县| 彭州市| 呼伦贝尔市| 晴隆县| 平乐县| 家居| 古田县| 哈巴河县| 海淀区| 黎城县| 泰顺县| 含山县| 金华市| 佳木斯市| 利辛县| 丘北县| 来凤县| 蕉岭县| 普兰店市| 临沧市| 景宁| 瑞安市| 长兴县| 项城市|