📜  c++ 共享指针运算符 bool - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:52.853000             🧑  作者: Mango

代码示例1
#include 
#include 
 
void report(std::shared_ptr ptr) {
    if (ptr) {
        std::cout << "*ptr=" << *ptr << "\n";
    } else {
        std::cout << "ptr is not a valid pointer.\n";
    }
}
 
int main() {
    std::shared_ptr ptr;
    report(ptr);
 
    ptr = std::make_shared(7);
    report(ptr);
}
//output:
//ptr is not a valid pointer.
//*ptr=7