C 學習筆記15 智慧型指標分析

2021-08-18 07:41:55 字數 621 閱讀 8662

智慧型指標的實現:

#include

using

namespace

std;

template

< typename t >

class smartpointer

// 實現一片堆空間只能由乙個智慧型指標標識

smartpointer(const smartpointer& obj)

// 也需要實現一片堆空間只能由乙個智慧型指標標識

smartpointer& operator = (const smartpointer& obj)

t* operator -> ()

t operator * ()

t* get()

~smartpointer()

};class test

~test()

};int main()

執行結果

test()

p = 0x2710f0

p = 0

p1 = 0x2710f0

~test()

智慧型指標的使用軍規:

C 智慧型指標學習筆記

原文 摘錄智慧型指標 auto ptr,unique ptr,shared ptr,weak ptr 智慧型指標的作用是管理乙個指標,因為存在一下的情況 申請的空間再函式結束時忘記釋放,造成記憶體洩漏。使用智慧型指標可以很大程度上的避免這個問題,因為智慧型指標是乙個類,當超出了類的例項物件的作用域時...

C 學習筆記之智慧型指標

眾所周知,c 中最讓程式設計師頭疼的就是關於記憶體的問題,其中不外乎以下幾點 1.緩衝區溢位 2.野指標 3.重複釋放記憶體 4.不配對的new delete 5.記憶體洩露 其中大多數的問題都是對指標的不正確使用帶來的。為此c 標準庫中對原始指標做了一些封裝,比如auto ptr,使得指標更容易使...

C 學習筆記 第37課 智慧型指標分析

include using namespace std class test intgetvalue intmain return0 執行結果 01234 解讀 沒有對p進行記憶體釋放,會導致記憶體洩漏 解決方法 智慧型指標分析 通過類物件模擬智慧型指標,只能用來指向堆空間中的物件或者變數,智慧型指...