Creating a Shared Pointer - C++ Data Type

C++ examples for Data Type:shared_ptr

Description

Creating a Shared Pointer

Demo Code

#include <iostream>
#include <memory>

using namespace std;

class MyClass/*from  w  w w. j  av a2  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 SharedMyClass = shared_ptr< MyClass >;

int main(int argc, char* argv[])
{
    SharedMyClass sharedMyClass{ new MyClass(10) };

    return 0;
}

Result


Related Tutorials