智慧型指標ScopedPtr

2021-07-10 15:13:51 字數 460 閱讀 5588

防拷貝的兩個條件:

1、保護限定符;

類外無法定義

2.、顯示進行宣告

不進行宣告,系統將會自動生成預設的,是淺拷貝

這種方法簡單,粗暴。不讓使用拷貝構造和賦值運算子過載,你進行使用會編譯錯誤

**如下:

templateclass scopedptr

~scopedptr() }

protected:

scopedptr(const scopedptr& ap);

scopedptr& operator=(const scopedptr& ap);

public:

t& operator*()

t* operator->()

t* getptr()

protected :

t* _ptr;

};

智慧型指標 scoped ptr

scoped ptr scoped ptr是乙個類似auto ptr的智慧型指標,它包裝了new操作符在堆上分配的動態物件,能夠保證動態建立的物件在任何時候都可以被正確的刪除。特徵 scoped ptr的所有權不能轉讓。操作函式 scoped ptr的建構函式接受乙個型別為t 的指標p,建立出乙個s...

Boost智慧型指標 scoped ptr

boost scoped ptr和std auto ptr非常類似,是乙個簡單的智慧型指標,它能夠保證在離開作用域後物件被自動釋放。下列 演示了該指標的基本應用 include include include class implementation void do something void t...

Boost智慧型指標 scoped ptr

boost scoped ptr和std auto ptr非常類似,是乙個簡單的智慧型指標,它能夠保證在離開作用域後物件被自動釋放。下列 演示了該指標的基本應用 include include include class implementation void do something void t...