MFC – CObject class – Enable Dynamic Object creation

As of now, we have discussed CObject class features “run-time class information” and “serialization support” in our previous articles MFC – CObject class – An Introduction – Enable Run-time class information and MFC – CObject class – Enable Object Serialization respectively. In this article, I am going to discuss CObject‘s another feature “dynamic object creation”.

To enable the “dynamic object creation” feature for the class which is derived from CObject class; we need to add the DECLARE_DYNCREATE macro in the class’ header file and we must also need to add the IMPLEMENT_DYNCREATE macro in the class’ implementation file. After adding these macros; MFC enables to use CRuntimeClass‘s CreateObject method.

The CreateObject method is useful to create dynamic objects for the given classes. This is a very helpful function; MFC internally uses this function to create class’ objects dynamically. CRuntimeClass provides multiple versions of the CreateObject function. One is its class member and another one is a static function. The syntax of these are:

CObject* CreateObject();

This method should be called using instance(s) of CRuntimeClass. To create a class from the string, we can use the below method. This is the static method of CRuntimeClass, so we can call this method directly; without using the CRuntimeClass instance.

static CObject* PASCAL CreateObject(LPCSTR lpszClassName);

Both these methods return a NULL value when failed to create an instance of a class.

Let’s put these macros into our class “Shape” and create an instance of this class dynamically.

Step 1. Add DECLARE_DYNCREATE macro into the “Shape” class header file. The syntax for this macro is:

DECLARE_DYNCREATE(<class name>)

Where <class name> is the name of the class.

After we added DECLARE_DYNCREATE macro to our class “Shape”; the class code looks like below:

// Shape.h
//
#include <afx.h>
#include <iostream>

class Shape : public CObject
{
public:
	Shape();

	DECLARE_DYNCREATE(Shape);
};

Step 2. Now the important thing is we need to add the IMPLEMENT_DYNCREATE macro into the “Shape” class’s implementation file. The syntax of IMPLEMENT_DYNCREATE macro is like below:

IMPLEMENT_DYNCREATE(<class_name>, <base class name>)

Where <class name> is the name of the class and <base class name> is its base class name. Our class name is “Shape”, so we need to pass Shape as <class name> and our “Shape” class’s base class is CObject; hence we need to pass CObject as <base class name>.

After adding this macro, the Shape class implementation file looks like below:

// Shape.cpp
//
#include "Shape.h"

using namespace std;

IMPLEMENT_DYNCREATE(Shape, CObject);

Shape::Shape()
{
	cout << "This is a Shape class." << endl;
}

Step 3. Our class Shape is enabled for dynamic object creation. That means we can create an instance of the Shape class dynamically. As discussed above, CRuntimeClass‘s CreateObject method(s) are useful to create a class’s instance dynamically. Here we will use both these methods to create instances of the Shape class. Usually, we use the below code to create an instance of the Shape class.

Shape obj;

This is the static creation of a class instance. To create an object dynamically:

Step 3.1. Create an instance of CRuntimeClass for Shape class.

CRuntimeClass *pRuntimeClass = RUNTIME_CLASS(Shape);

Step 3.2. Once we have CRuntimeClass‘s instance; call its CreateObject method to create an instance of Shape class.

pRuntimeClass->CreateObject();

OR

Step 3.1. Call CRuntimeClass‘s static version of CreateObject method to create an instance of Shape class dynamically.

CRuntimeClass::CreateObject("Shape");

Observe that here we are passing name of the class as a string.

Upon success; both above methods will return an instance pointer to CObject class.

Putting together the dynamic object creation code looks like below:

void main()
{
	// Method 1 - Object creation
	CObject *pobj1 = NULL;

	CRuntimeClass *pRuntimeClass = RUNTIME_CLASS(Shape);
	if ( pRuntimeClass != NULL )
		pobj1 = pRuntimeClass->CreateObject();

	if ( pobj1 != NULL )
	{
		if ( pobj1->IsKindOf(RUNTIME_CLASS(Shape)) )
			printf("This is an instance of Shape class.");

		delete pobj1;
	}

	// Method 2 - Object creation using static method
	CObject *pobj2 = CRuntimeClass::CreateObject("Shape");

	if ( pobj2 != NULL )
	{
		if ( pobj2->IsKindOf(RUNTIME_CLASS(Shape)) )
			printf("This is an instance of Shape class.");

		delete pobj2;
	}
}

From the above code, we have created a Shape class object dynamically created using CRuntimeClass‘s CreateObject method(s). Method 2 may not create an instance for Shape class; by enabling serialization support to Shape class to make it work.

Let’s discuss CObject‘s other feature “object diagnostic support” in our next article “MFC – CObject class – Enable Object Diagnostic support“.

**

MFC – CObject class – Enable Dynamic Object creation

Leave a Reply

Your email address will not be published.

Scroll to top