Thursday 21 January 2016

BOOST shared_ptr example

#include <iostream>
#include <memory>

using namespace std;

class C
{
        public:
                int a;
                int b;

                C(){cout<<"Constructor called"<<endl;}
                ~C(){cout<<"Destructor called"<<endl;}
                int add(int a, int b)
                {

                }
};

int main()
{
        C * pC = new C;
        shared_ptr<C> scp(pC);
        scp->a = 10;
        scp->b = 20;

        shared_ptr<C> scp1(scp);
        cout<<"a: "<<scp->a<<endl;
        cout<<"b: "<<scp->b<<endl;

        scp.reset();

        cout<<"a: "<<pC->a<<endl;
        cout<<"b: "<<pC->b<<endl;

        scp1.reset();

        cout<<"a: "<<pC->a<<endl;
        cout<<"b: "<<pC->b<<endl;
}

No comments:

Post a Comment