Microsoft
Software
Hardware
Network
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/lib
rary/
5fsfk
9dy(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_Tes
tView)
// Attributes
public:
CMFC_TestDoc* GetDocument();
...
=========================
class CMFC_TestView : public CView
{
protected: // create from serialization only
// CMFC_TestView();
DECLARE_DYNCREATE(CMFC_Tes
tView)
// 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.
Random Solutions
Connection String for MS Access database outsiide wwwroot
ReportViewer Export to PDF Loses Tabs or Multiple Blnak Spaces
Error 438 Obejct doesn't support property or method
Visual Studio 2008, Listbox location on picturebox insdie a form
How do I convert a null value to a blank space?
ListView Column Sort By Click Natural Sorting
Password protect adobe 9
WinPE, SysPrep, Unattened - Remote Deployments
How can I enable a template item accoding to query result ?
Retrieve drivers before wipe?