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

首頁 > 學院 > 開發設計 > 正文

13.2.1

2019-11-14 09:48:26
字體:
來源:轉載
供稿:網友

13.23 有一定差異,我使用if判斷是否自我拷貝 本節使用先拷貝右側對象的方式來使自我拷貝正常運行

13.24 如果沒有定義析構函數,肯定會發生內存泄露 如果沒有定義拷貝構造函數,當使用了拷貝構造函數時,刪除一個對象會使和其他對象共用的內存被釋放,其他對象則出現錯誤

13.25 拷貝構造和拷貝賦值必須新建內存,而不是和右側共用內存

當shared_ptr計時器為0時,會自動的釋放,所以不需要析構函數

13.26 @pezy

https://github.com/PYPARA/Cpp-PRimer/blob/master/ch13/ex13_26.h

#include <vector>#include <string>#include <initializer_list>#include <memory>#include <exception>using std::vector; using std::string;class ConstStrBlobPtr;class StrBlob {public: using size_type = vector<string>::size_type; friend class ConstStrBlobPtr; ConstStrBlobPtr begin() const; ConstStrBlobPtr end() const; StrBlob():data(std::make_shared<vector<string>>()) { } StrBlob(std::initializer_list<string> il):data(std::make_shared<vector<string>>(il)) { } // copy constructor StrBlob(const StrBlob& sb) : data(std::make_shared<vector<string>>(*sb.data)) { } // copyassignment Operators StrBlob& operator=(const StrBlob& sb); 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() { check(0, "pop_back on empty StrBlob"); data->pop_back(); } std::string& front() { check(0, "front on empty StrBlob"); return data->front(); } std::string& back() { check(0, "back on empty StrBlob"); return data->back(); } const std::string& front() const { check(0, "front on empty StrBlob"); return data->front(); } const std::string& back() const { check(0, "back on empty StrBlob"); return data->back(); }private: void check(size_type i, const string &msg) const { if (i >= data->size()) throw std::out_of_range(msg); }private: std::shared_ptr<vector<string>> data;};class ConstStrBlobPtr {public: ConstStrBlobPtr():curr(0) { } ConstStrBlobPtr(const StrBlob &a, size_t sz = 0):wptr(a.data), curr(sz) { } // should add const bool operator!=(ConstStrBlobPtr& p) { return p.curr != curr; } const string& deref() const { // return value should add const auto p = check(curr, "dereference past end"); return (*p)[curr]; } ConstStrBlobPtr& incr() { check(curr, "increment past end of StrBlobPtr"); ++curr; return *this; }private: std::shared_ptr<vector<string>> check(size_t i, const string &msg) const { auto ret = wptr.lock(); if (!ret) throw std::runtime_error("unbound StrBlobPtr"); if (i >= ret->size()) throw std::out_of_range(msg); return ret; } std::weak_ptr<vector<string>> wptr; size_t curr;};ConstStrBlobPtr StrBlob::begin() const // should add const{ return ConstStrBlobPtr(*this);}ConstStrBlobPtr StrBlob::end() const // should add const{ return ConstStrBlobPtr(*this, data->size());}StrBlob& StrBlob::operator=(const StrBlob& sb){ data = std::make_shared<vector<string>>(*sb.data); return *this;}int main(){ return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 夏津县| 衡阳县| 双柏县| 大足县| 三门峡市| 九龙坡区| 兴安县| 江油市| 海宁市| 正阳县| 潮州市| 镶黄旗| 镇江市| 奈曼旗| 博野县| 津南区| 葵青区| 江陵县| 綦江县| 五原县| 广汉市| 灌南县| 剑阁县| 健康| 皋兰县| 昆山市| 孝感市| 宜阳县| 博兴县| 阿城市| 团风县| 西峡县| 佛冈县| 宁强县| 札达县| 莱芜市| 西藏| 鞍山市| 荆州市| 镇康县| 轮台县|