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
Import data from excel then add autonumber field: 90000 rows
Explorer.EXE - Unable To Locate Component
user can not login after the complete restore of AD
Windows Server 2003 SP2 can't see 5.4 terabyte array
There is insufficient system memory in resource pool 'internal' to run this query. WE upgraded from a 32 bit, 4 GB, Windows 2003 server, SQL server 2005 machine to a 64 bit, 2 processor quad core, 8
Excel Macro to Compare Cells and Edit the Workbook
.NET CF Performance counters are missing
"Data type mismatch in criteria expression." when comparing dates
WINDOWS SERVER 2008 BACKUP
SQL Syntax