shared_ptr can give you shared ownership over a single object. - C++ Data Type

C++ examples for Data Type:shared_ptr

Description

shared_ptr can give you shared ownership over a single object.

Demo Code

#include <iostream>
#include <memory>
#include <string>
using namespace std;

class MyClass/*from w w w . j  a v  a2s  .  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 MySharedPtr = shared_ptr<MyClass>;

auto PassSharedPtr(MySharedPtr ptr)
{
    cout << "In Function Name: " << ptr->GetName() << endl;
    return ptr;
}

int main()
{
    auto sharedPointer = make_shared<MyClass>("MyClass", 10);
    {
        auto newSharedPointer = PassSharedPtr(sharedPointer);
        if (sharedPointer)
        {
            cout << "First Object Name: " << sharedPointer->GetName() << endl;
        }

        cout << "Second Object Name: " << newSharedPointer->GetName() << endl;
    }

    return 0;
}

Result


Related Tutorials