c++ shared pointer code example
Example 1: c++ shared pointer
#include <memory>
std::shared_ptr<int> foo = std::make_shared<int> (10);
Example 2: c++ shared pointer operator bool
#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);
}
//output:
//ptr is not a valid pointer.
//*ptr=7