|
Question : DECLARE_DYNCREATE and protected constructor
|
|
Hello, experts: I search DECLARE_DYNCREATE from MSDN, I don't understand the following description: ---------------------------------------------------------------------------- http://msdn2.microsoft.com/fr-fr/library/5fsfk9dy(VS.80).aspx Note that this macro definition will invoke the default constructor for your class. If a non-trivial constructor is explicitly implemented by the class, it must also explicitly implement the default constructor as well. The default constructor can be added to the class's private or protected member sections to prevent it from being called from outside the class implementation. Example: /* CAge.h */ class CAge : public CObject { int m_Age; public: DECLARE_DYNCREATE(CAge) CAge( int Age ) { m_Age = Age; } private: CAge() { m_Age = 0; } // provide default constructor only for // dynamic creation };
//============== /* CAge.cpp */ #include "stdafx.h" #include "CAge.h"
IMPLEMENT_DYNCREATE(CAge, CObject)
---------------------------------------------------------------------------- At the above class, I know that the following declarations are wrong: CAge obj1; // (x) CAge *obj2 = new CAge; // (x)
But I don't know why we have to put default constructor into class' private or protected member sections when DECLARE_DYNCREATE is included in the class declaration. I create a MFC Appizard(exe) SDI project, I try to put the protected constructor into public section. No error happens.Suhc as ========================= class CMFC_TestView : public CView { protected: // create from serialization only CMFC_TestView(); DECLARE_DYNCREATE(CMFC_TestView)
// Attributes public: CMFC_TestDoc* GetDocument(); ... ========================= class CMFC_TestView : public CView { protected: // create from serialization only // CMFC_TestView(); DECLARE_DYNCREATE(CMFC_TestView)
// Attributes public: CMFC_TestDoc* GetDocument(); CMFC_TestView();
... ========================= Could someone tell me why we have to put default constructor into class' private or protected member sections ? Thank you.
|
|
Answer : DECLARE_DYNCREATE and protected constructor
|
|
We MUST have default constructor, but it can be in any section. You CAN put it to private or protected section, but you don't HAVE to do this.
|
|
|
|