Originally Posted by
laserlight
Is isB() really necessary? It pretty much means that the base class must know about one of its child classes. If you can get away without it, that would be better.
The only reason isB() exists is so D objects will know whether x() points to a B or C. What would be a better way to test this?
Originally Posted by
laserlight
It should be an A*.
A pointer to a base class can also point to any derived classes as well without breaking things? If I'm understanding you correctly, and use A* as the data type for D::x and return type for D::x(), then the following should be possible, right?
Code:
B *b = new B();
C *c = new C();
D *d = new D();
// point x to b
d->x ( b );
cout << d->x->some_class_B_method () << endl;
// point x to c
d->x ( c );
cout << d->x->some_class_C_method () << endl;