#include #include "ref_count.h" using std::cout; using std::endl; // A <-- B X <-- Y struct A { int x; A( int y ) : x(y) {} virtual void f() { cout << x << " A::f()" << endl; } virtual ~A() { cout << x << " A's destructor" << endl; } }; struct B : public A { B( int y ) : A(y) {} void f() const { cout << x << " B::f()" << endl; } ~B() { cout << x << " B's destructor" << endl; } }; namespace fcpp {} using namespace fcpp; int main() { Ref a; { Ref b( new B(2) ); a = b; //b = a; // illegal } a->f(); Ref b2 = ref_dynamic_cast( a ); b2->f(); cout << "----------------" << endl; a = Ref( new A(1) ); a->f(); b2->f(); cout << "----------------" << endl; b2 = ref_dynamic_cast( a ); if( !b2 ) { cout << "Good, should be NULL, and #2 should be dead already" << endl; } return 0; }