c 2b 2b shared pointer operator bool

Solutions on MaxInterview for c 2b 2b shared pointer operator bool by the best coders in the world

showing results for - "c 2b 2b shared pointer operator bool"
Noah
28 May 2018
1#include <iostream>
2#include <memory>
3 
4void report(std::shared_ptr<int> ptr) {
5    if (ptr) {
6        std::cout << "*ptr=" << *ptr << "\n";
7    } else {
8        std::cout << "ptr is not a valid pointer.\n";
9    }
10}
11 
12int main() {
13    std::shared_ptr<int> ptr;
14    report(ptr);
15 
16    ptr = std::make_shared<int>(7);
17    report(ptr);
18}
19//output:
20//ptr is not a valid pointer.
21//*ptr=7