We help IT Professionals succeed at work.
Get Started

DECLARE_DYNCREATE and protected constructor

TKD
TKD asked
on
1,140 Views
Last Modified: 2013-11-20
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.
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE