It's okay, the STL has you covered:
#include <memory>#include <iostream>struct MyObject{ MyObject() { std::cout << "new " << this << std::endl; } MyObject( MyObject const & ref ) { std::cout << "new " << this << " (from " << &ref << ")" << std::endl; } ~MyObject() { std::cout << "del " << this << std::endl; }};struct foo{ typedef std::auto_ptr<MyObject> MyObjectPointer; MyObjectPointer p; foo(): p( new MyObject ) {} foo & operator=( foo const & rhs ) { if ( this != &rhs ) p = MyObjectPointer(new MyObject( *rhs.p )); return *this; } foo( foo const & rhs ) { *this = rhs; }};int main(){ foo a, b, c = a; a = b = c;}
$ g++ -ggdb3 -Wall -Wextra -Werror -ansi -pedantic test.cpp -o test
$ valgrind ./test
<snip>new 0x1bb36028
new 0x1bb36060
new 0x1bb36098 (from 0x1bb36028)
new 0x1bb360d0 (from 0x1bb36098)
del 0x1bb36060
new 0x1bb36108 (from 0x1bb360d0)
del 0x1bb36028
del 0x1bb36098
del 0x1bb360d0
del 0x1bb36108
==21675==
==21675== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 1)
==21675== malloc/free: in use at exit: 0 bytes in 0 blocks.
==21675== malloc/free: 5 allocs, 5 frees, 5 bytes allocated.
==21675== For counts of detected errors, rerun with: -v
==21675== No malloc'd blocks -- no leaks are possible.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.