consider following class definitions:
#include <string> class cparty { public: cparty(); virtual ~cparty(); int m_nid; }; class cpartyex : public cparty { public: cpartyex(); ~cpartyex(); std::string m_sname; }; class ctransaction { public: ctransaction(); virtual ~ctransaction(); int m_nclassno; }; class ctransactionex : public ctransaction { public: ctransactionex(); ~ctransactionex(); std::string m_sdesc; }; class cobject { public: cobject(); ~cobject(); cparty* m_pparent; ctransaction* m_ptransaction; };
in appropriate implementation, create following types of cobject storage objects:
// ok: basic party basic transaction cobject iobject1; iobject1.m_pparent = new cparty(); iobject1.m_ptransaction = new ctransaction(); // ok: extended party extended transaction cobject iobject2; iobject2.m_pparent = new cpartyex(); iobject2.m_ptransaction = new ctransactionex();
however, current cobject definition not prevent mixing cparty , ctransaction types incompatible 1 another:
// not intended: basic party extended transaction cobject iobject3; iobject3.m_pparent = new cparty(); iobject3.m_ptransaction = new ctransactionex(); // not intended: extended party basic transaction cobject iobject4; iobject4.m_pparent = new cpartyex(); iobject4.m_ptransaction = new ctransaction();
is somehow possible place restriction on how 2 objects can linked inside cobject instance?
you encapsulate decision in cobject
constructor:
class cobject { public: cobject(bool extended = false); }; cobject::cobject (bool extended) { if (extended) { m_pparent = new cpartyex(); m_ptransaction = new ctransactionex(); } else { m_pparent = new cparty(); m_ptransaction = new ctransaction(); } }
Comments
Post a Comment