Thursday, 21 January 2016

BOOST scoped_ptr example

#include <iostream>
#include <boost/scoped_ptr.hpp>

using namespace std;
using namespace boost;

class C
{
        public:
                int a;
                int b;

                C(){cout<<"Constructor called"<<endl;}
                ~C(){cout<<"Destructor called"<<endl;}
};

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

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

        scp.reset();

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

No comments:

Post a Comment