User:Ccw
Jump to navigation
Jump to search
Some class constructors and destructors are too complex
Proposed solution to [issue http://bugs.mtasa.com/view.php?id=4377]
Move code out of constructors and destructors into functions that are called post-construction and pre-destruction respectively.
Example of solution when applied to CClientEntity and CClientColModel:
Current base and concrete class declaration:
class CClientEntity { public: CClientEntity ( ElementID ID ); virtual ~CClientEntity ( void ); ... class CClientColModel: public CClientEntity { public: CClientColModel ( class CClientManager* pManager, ElementID ID ); ~CClientColModel ( void ); ...
Proposed base class declaration and definition:
class CClientEntity { protected: CClientEntity ( void ) {} ~CClientEntity ( void ) {} void PostConstruct ( ElementID ID ); virtual void PreDestroy ( void ); public: void Delete ( void ); ... void CClientEntity::PostConstruct ( ElementID ID ) { // Code from CClientEntity constructor moved here } void CClientEntity::PreDestroy () { // Code from CClientEntity destructor moved here } void CClientEntity::Delete ( void ) { PreDestroy (); delete this; }
Proposed concrete class declaration and definition:
class CClientColModel: public CClientEntity { protected: CClientColModel ( void ) {} ~CClientColModel ( void ) {} void PostConstruct ( class CClientManager* pManager, ElementID ID ); virtual void PreDestroy ( void ); public: static CClientColModel* NewCClientColModel ( CClientStreamer * pStreamer, ElementID ID ); ... void CClientColModel::PostConstruct ( CClientStreamer * pStreamer, ElementID ID ) { CClientEntity::PostConstruct ( ID ); // Code from CClientColModel constructor moved here } void CClientColModel::PreDestroy () { // Code from CClientColModel destructor moved here __super::PreDestroy (); } CClientColModel* CClientColModel::NewCClientColModel ( CClientStreamer * pStreamer, ElementID ID ) { CClientColModel* pElement = new CClientColModel (); pElement->PostConstruct ( pStreamer, ID ) return pElement; }
Other changes required
Any occurrence of
new CClientColModel ( pStreamer, ID ); delete pElement;
should be replaced with
CClientColModel::NewCClientColModel ( pStreamer, ID ); pElement->Delete ();