Some clarification I think, is needed.
when class B is derived from class A, the following holds:
- a B is an A
- an A is not necessarily a B
if you are holding a pointer to A, you can check whether it is the A-part of a B by attempting to dynamic_cast to a B. If that cast succeeds, it is because you were actually pointing to the B's A interface. If the cast does not succeed then the A-like thing you were pointing at was not actually a B.
So:
struct A {
virtual ~A();
}; // a base class
struct B : A {}; // a B is an A but an A is not necessarily a B
A* pa = new A;
// pa is pointing to an A
B* pb = dynamic_cast<B*>(pa);
// pb will now be NULL because this cast will have failed.
// Remember, a B *is an* A, but an A *is not necessarily a* B
delete pa;
// try again...
pa = new B;
// pa points to the A-part of a B, so
pb = dynamic_cast<B*>(pa);
// pb now points to the same object, but has access to all the methods on the B interface
delete pb;
// clean up