- Examples
The following examples explain common usages of smart pointers in embedded programming.
We'll cover the following...
Example 1 #
To get a visual idea of the life cycle of the resource, there is a short message in the constructor and destructor of MyInt
(lines 8 - 16).
Press + to interact
// sharedPtr.cpp#include <iostream>#include <memory>using std::shared_ptr;struct MyInt{MyInt(int v):val(v){std::cout << " Hello: " << val << std::endl;}~MyInt(){std::cout << " Good Bye: " << val << std::endl;}int val;};int main(){std::cout << std::endl;shared_ptr<MyInt> sharPtr(new MyInt(1998));std::cout << " My value: " << sharPtr->val << std::endl;std::cout << "sharedPtr.use_count(): " << sharPtr.use_count() << std::endl;{shared_ptr<MyInt> locSharPtr(sharPtr);std::cout << "locSharPtr.use_count(): " << locSharPtr.use_count() << std::endl;}std::cout << "sharPtr.use_count(): "<< sharPtr.use_count() << std::endl;shared_ptr<MyInt> globSharPtr= sharPtr;std::cout << "sharPtr.use_count(): "<< sharPtr.use_count() << std::endl;globSharPtr.reset();std::cout << "sharPtr.use_count(): "<< sharPtr.use_count() << std::endl;sharPtr= shared_ptr<MyInt>(new MyInt(2011));std::cout << std::endl;}
Explanation #
-
In line 22, we create
MyInt(1998)
, which is the resource that the smart pointer should address. By using ...