#include <iostream>
#include <new>
using namespace std;
template < typename T>
class SmartWrapper
{
private :
T * obj;
public :
SmartWrapper( T * newObj)
{
cout << "Wrapper: Begin" << endl;
obj = newObj;
}
~SmartWrapper( )
{
delete obj;
cout << "Wrapper: End" << endl;
}
} ;
class Person
{
public :
Person( )
{
cout << "Person: Begin" << endl;
}
~Person( )
{
cout << "Person: End" << endl;
}
} ;
class Dog
{
public :
Dog( )
{
cout << "Dog: Begin" << endl;
}
~Dog( )
{
cout << "Dog: End" << endl;
}
} ;
int main( )
{
// we should have the same number of begins and ends.
// we actually have half of ends because objects created
// without smart pointers are not deleted after each iteration.
for ( int i = 0 ; i < 2 ; i++ )
{
Dog* c1 = new Dog( ) ;
Person* c2 = new Person( ) ;
SmartWrapper< Dog> s1( new Dog( ) ) ;
SmartWrapper< Person> s2( new Person( ) ) ;
}
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bmV3PgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnRlbXBsYXRlIDx0eXBlbmFtZSBUPgpjbGFzcyBTbWFydFdyYXBwZXIKewogICAgcHJpdmF0ZToKICAgIFQgKiBvYmo7CiAgICBwdWJsaWM6CiAgICBTbWFydFdyYXBwZXIoVCAqIG5ld09iaikKICAgIHsKICAgICAgICBjb3V0IDw8ICJXcmFwcGVyOiBCZWdpbiIgPDwgZW5kbDsKICAgICAgICBvYmogPSBuZXdPYmo7CiAgICB9CiAgICB+U21hcnRXcmFwcGVyKCkKICAgIHsKICAgICAgICBkZWxldGUgb2JqOwogICAgICAgIGNvdXQgPDwgIldyYXBwZXI6IEVuZCIgPDwgZW5kbDsKICAgIH0KfTsKCmNsYXNzIFBlcnNvbgp7CiAgICBwdWJsaWM6CiAgICBQZXJzb24oKQogICAgewogICAgICAgIGNvdXQgPDwgIlBlcnNvbjogQmVnaW4iIDw8IGVuZGw7CiAgICB9CiAgICB+UGVyc29uKCkKICAgIHsKICAgICAgICBjb3V0IDw8ICJ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