智慧型指標派生類 unique ptr STL原始碼

2021-10-02 09:18:56 字數 4238 閱讀 3839

// unique_ptr指標(唯一指向資源的指標,每當賦值、構造時,轉讓右側指標資料到左側,右側為空,即同一時間只有乙個指標指向資料)

// 其中,千萬別在定義乙個unique_ptr指標時出現下述情況已知unique_ptrq(new int(1));q也可以是shared_ptr型別

// unique_ptrp(q.get());這是合法且編譯通過(但是就出現了兩智慧型指標指向同一記憶體的現象),所以當p離開其作用域且q依舊在其作用域範圍內時,p的釋放導致q為懸空指標,q釋放時就會導致重複釋放而引起的錯誤。

// 例如:

std::unique_ptrq(new int(111111));

//std::shared_ptrqq = std::make_shared(2222);

// template class unique_ptr scalar

template// = default_delete<_ty>

class unique_ptr

: private _unique_ptr_base<_ty, _dx,

is_empty<_dx>::value // 非空 或是 預設刪除器 則為true(特化版)

|| is_same, _dx>::value>

unique_ptr(nullptr_t) _noexcept

: _mybase(pointer())

// 不允許指標之間的賦值,但可以賦值為0

_myt& operator=(nullptr_t) _noexcept

explicit unique_ptr(pointer _ptr) _noexcept

: _mybase(_ptr)

unique_ptr(pointer _ptr,

typename _if::value, _dx,

const typename remove_reference<_dx>::type&>::type _dt) _noexcept

: _mybase(_ptr, _dt)

unique_ptr(pointer _ptr,

typename remove_reference<_dx>::type&& _dt) _noexcept

: _mybase(_ptr, _std move(_dt))

// 移動copy

unique_ptr(unique_ptr&& _right) _noexcept

: _mybase(_right.release(),// 釋放右側無名物件

_std forward<_dx>(_right.get_deleter()))

template::value

&& is_convertible::pointer,

pointer>::value

&& ((is_reference<_dx>::value && is_same<_dx, _dx2>::value)

|| (!is_reference<_dx>::value

&& is_convertible<_dx2, _dx>::value)),

void>::type>

unique_ptr(unique_ptr<_ty2, _dx2>&& _right) _noexcept

: _mybase(_right.release(),

_std forward<_dx2>(_right.get_deleter()))

template::value

&& is_same<_dx, default_delete<_ty> >::value,

void>::type>

unique_ptr(auto_ptr<_ty2>&& _right) _noexcept

: _mybase(_right.release())

templatetypename enable_if::value

&& is_convertible::pointer,

pointer>::value,

_myt&>::type

operator=(unique_ptr<_ty2, _dx2>&& _right) _noexcept

// 移動賦值

_myt& operator=(_myt&& _right) _noexcept

return (*this);

} void swap(_myt& _right) _noexcept

~unique_ptr() _noexcept

typename add_reference<_ty>::type operator*() const

pointer operator->() const _noexcept

pointer get() const _noexcept

explicit operator bool() const _noexcept

// 放棄對資源的控制,返回資源指標

pointer release() _noexcept

// 建立新的指標資料,釋放old指標資料

void reset(pointer _ptr = pointer()) _noexcept

// 定義為delete,不允許copy構造和賦值構造

unique_ptr(const _myt&) = delete;

_myt& operator=(const _myt&) = delete;

};// template class unique_ptr array

// 指標陣列模板類

templateclass unique_ptr<_ty, _dx>

: private _unique_ptr_base<_ty, _dx,

is_empty<_dx>::value

|| is_same, _dx>::value>

explicit unique_ptr(pointer _ptr) _noexcept

: _mybase(_ptr)

unique_ptr(pointer _ptr,

typename _if::value, _dx,

const typename remove_reference<_dx>::type&>::type _dt) _noexcept

: _mybase(_ptr, _dt)

unique_ptr(pointer _ptr,

typename remove_reference<_dx>::type&& _dt) _noexcept

: _mybase(_ptr, _std move(_dt))

unique_ptr(unique_ptr&& _right) _noexcept

: _mybase(_right.release(),

_std forward<_dx>(_right.get_deleter()))

_myt& operator=(_myt&& _right) _noexcept

return (*this);

} unique_ptr(nullptr_t) _noexcept

: _mybase(pointer())

_myt& operator=(nullptr_t) _noexcept

void reset(nullptr_t) _noexcept

void swap(_myt& _right) _noexcept

~unique_ptr() _noexcept

// 過載

typename add_reference<_ty>::type operator(size_t _idx) const

pointer get() const _noexcept

explicit operator bool() const _noexcept

pointer release() _noexcept

void reset(pointer _ptr = pointer()) _noexcept

templateexplicit unique_ptr(_ptr2) = delete;

templateunique_ptr(_ptr2, _dx2) = delete;

unique_ptr(const _myt&) = delete;

_myt& operator=(const _myt&) = delete;

templatevoid reset(_ptr2) = delete;

private:

void _delete()

};

智慧型指標派生類 weak ptr STL原始碼

template class weak ptr weak ptr智慧型指標類模板 指向由智慧型指標管理的物件,增加弱引用計數,若為shared ptr不增加的引用計數 非弱 templateclass weak ptr public ptr base ty 繼承自智慧型指標基類 copy建構函式 w...

關於基類指標 派生類指標 基類物件派 生類物件問題

1 基類指向派生類的指標 2 派生類指向基類的指標 3 以及將派生類指標強制轉換為基類指標 4 將基類指標強制轉換為派生類指標 無論是基類指向派生類還是派生類指向基類,重點就是哪個類的指標就呼叫哪個類的方法,而輸出的是指標指向的物件。基類指向派生類的指標即將派生類物件賦給基類指標,如果輸出的話,呼叫...

基類指標指向派生類

include class a virtual void vt private void a2 protected void a3 class b public a virtual void vt private void b2 protected void b3 int main 結論 1 對於派...