共用方式為


shared_ptr::operator boolean-type

測試,如果資源存在。

operator boolean-type() const;

備註

運算子會傳回可以轉換為 bool型別的值。 轉換的結果。 bool 的是 true ,當 get() != 0,則為 false。

範例

 

// std_tr1__memory__shared_ptr_operator_bool.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
int main() 
    { 
    std::shared_ptr<int> sp0; 
    std::shared_ptr<int> sp1(new int(5)); 
 
    std::cout << "(bool)sp0 == " << std::boolalpha 
        << (bool)sp0 << std::endl; 
    std::cout << "(bool)sp1 == " << std::boolalpha 
        << (bool)sp1 << std::endl; 
 
    return (0); 
    } 
 
  

需求

標題: <memory>

命名空間: std

請參閱

參考

shared_ptr Class

shared_ptr::get