std::shared_ptr<T>::operator bool
來自 cppreference.com
< cpp | 記憶體 | shared ptr
explicit operator bool() const noexcept; |
||
檢查 *this 是否儲存非空指標,即 get() != nullptr 是否為真。
目錄 |
[編輯] 引數
(無)
[編輯] 返回值
如果 *this 儲存指標,則返回 true,否則返回 false。
[編輯] 注意
一個空的 shared_ptr(其中 use_count() == 0)可能儲存一個可透過 get() 訪問的非空指標,例如如果它是使用別名建構函式建立的。
[編輯] 示例
執行此程式碼
#include <iostream> #include <memory> void report(std::shared_ptr<int> ptr) { if (ptr) std::cout << "*ptr=" << *ptr << "\n"; else std::cout << "ptr is not a valid pointer.\n"; } int main() { std::shared_ptr<int> ptr; report(ptr); ptr = std::make_shared<int>(7); report(ptr); }
輸出
ptr is not a valid pointer. *ptr=7
[編輯] 參閱
返回儲存的指標 (public member function) |