Creating Single-Instance Dynamic Objects using a unique_ptr - C++ Data Type

C++ examples for Data Type:unique_ptr

Description

Creating Single-Instance Dynamic Objects using a unique_ptr

Demo Code

#include <iostream>
#include <memory>

using namespace std;

class MyClass/* ww w  .  j a va 2 s. c  o  m*/
{
private:
    int m_Number{ 0 };

public:
    MyClass(int value) : m_Number{ value }{
    }

    ~MyClass(){
        cout << "Destroying " << m_Number << endl;
    }

    void operator=(const int value){
        m_Number = value;
    }

    int GetNumber() const{
        return m_Number;
    }
};
using UniqueMyClass = unique_ptr< MyClass >;

void CreateUniqueObject()
{
    UniqueMyClass uniqueMyClass{ make_unique<MyClass>(10) };
}

int main(int argc, char* argv[])
{
    cout << "Begin Main!" << endl;

    CreateUniqueObject();

    cout << "Back in Main!" << endl;

    return 0;
}

Result


Related Tutorials