《C 沉思錄》 第七章 控制代碼 第二部分

2021-06-19 14:46:32 字數 1384 閱讀 8798

第六章談及一種向類中新增控制代碼和引用計數的技術,以能夠通過值控制引用計數就能夠高效地「複製」該類的物件。這種技術有乙個明顯的缺點:為了把控制代碼繫結到類t的物件上,必須定義乙個具有型別t的成員的新類。

分離引用計數:

class handle

handle(int x, int y): u(new int(1)), p(new point(x, y)) {}

handle(const point& p0): u(new int(1)), p(new point(p0)) {}

handle(const handle& h): u(h.u), p(h.p)

handle& operator=(const handle& h)

u = h.u;

p = h.p;

return *this;

}~handle()

}private:

point* p;

int* u; //指向引用計數的指標

};

對引用計數的抽象:

class usecount

usecount(const usecount& uc):count(uc.count)

usecount& operator=(const usecount &u)

~usecount()

}bool only() //判斷是否只指向乙個計數,用於判斷是否要刪除

bool reattach(const usecount &u) //重新連線,用於複製

count = u.count;

return false;

}bool makeonly() //分配乙個新的,用於寫時複製技術

private:

int *count;//計數

};class handle

handle(int x, int y): p(new point(x, y)) {}

handle(const point& p0): p(new point(p0)) {}

handle(const handle& h): u(h.u), p(h.p) {}

handle& operator=(const handle& h)

p = h.p;

return *this;

}~handle()

}int x() const

handle& x(int x0)

p->x(x0);

return *this;

}private:

point* p;

usecount* u; //指向引用計數的指標

};

第二部分 第七章 Linux檔案和目錄管理

第二部分 第七章 linux檔案和目錄管理 1.目錄和檔案 1 絕對路徑 略 2 相對路徑 2.目錄的相關操作 1 表示此層目錄 2 表示上一層目錄 3 表示前乙個工作目錄 4 表示 當前使用者身份 所在的家目錄 5 account 表示account使用者的家目錄 目錄操作常用指令 cd chan...

C 階段總結第二部分

目錄 第二部分 c 核心程式設計一 1,記憶體分割槽模型 1.1程式執行前 1.2程式執行後 1.2new操作符 2.引用 2.1引用的基本使用 2.2注意事項 2.3引用做函式引數 2.4引用做函式返回值 2.5引用的本質 2.6常量引用 3,函式的提高 3.1函式的預設引數 3.2函式佔位引數 ...

c 學習筆記第二部分

part 2 一 陣列 指標 在c 中的陣列std string str string是std類裡面的乙個物件 這個語句就是命名乙個陣列名字叫做str 2.c 允許指標群 p,就是多個指標有同樣的值 int p1 myint int p2 myint c 支援無型別 void 指標,就是沒有被宣告為...