C++ examples for Data Type:unique_ptr
Pass a unique_ptr around your program.
#include <iostream> #include <memory> #include <string> using namespace std; class MyClass/*from ww w .j a v a 2 s . c o m*/ { private: string m_Name; int m_Value; public: MyClass(const string& name, int value) : m_Name{ name } , m_Value{ value } { cout << "Constructing!" << endl; } ~MyClass() { cout << "Destructing!" << endl; } const string& GetName() const { return m_Name; } int GetValue() const { return m_Value; } }; using MyUniquePtr = unique_ptr<MyClass>; auto PassUniquePtr(MyUniquePtr ptr) { cout << "In Function Name: " << ptr->GetName() << endl; return ptr; } int main() { auto uniquePointer = make_unique<MyClass>("MyClass", 10); auto newUniquePointer = PassUniquePtr(move(uniquePointer)); if (uniquePointer) { cout << "First Object Name: " << uniquePointer->GetName() << endl; } cout << "Second Object Name: " << newUniquePointer->GetName() << endl; return 0; }