Design Alternatives and Perspectives ◾ 243
Example 8.10: Polymorphic Subobjects: C++
class flexQueen
{
B* subObjP;
public:
flexQueen()
{ // subObjP = 0;
subObjP = new B;
// subObjP = new Be;
// subObjP = new Bee;
// subObjP = new B[size];
… // other choices
}
virtual void store()
{ subObjP->store(); // dynamic behavior
…
}
void replaceB(B*& worker)
{ delete subObjP;
subObjP = worker;
worker = 0;
}
…
};
Despite its silly name, the QueenB example illustrates simple design
variants that impact control and exibility as well as manipulation via
interfaces. Consider a hierarchy of B types (B, Be, Bee, B e N ot,...),where
each subtype can override the inherited virtual store() method. ...