CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

MFC – CObject class – An Introduction – Enable Run-time class information

CObject is the base class for almost all classes in Microsoft Foundation Class (MFC) library. A class is qualified as a MFC based class by deriving its from MFC’s base class CObject.

Once the normal class is derived from CObject class; it inherits the following services offered by CObject class.

  • Run-time class information
  • Serialization support
  • Dynamic creation support
  • Object diagnostic output

In this series of articles, we will look about these with examples.

Run-time class information is useful to get the type of the class the object associates at run-time. By default, this functionality will not be available to the class, even though the class is derived from CObject class.

Lets take a simple example. Here we will use a simple class developed in “MFC: Convert C++ Class to MFC based class”.

Lets use CObject‘s member to check whether class of give type. The code looks like below:

void main()
{
	Shape shape;

	if ( shape.IsKindOf(RUNTIME_CLASS(Shape)) == TRUE )
	{
		cout << "This is of type Shape." << endl;
	}
}

We have used the function IsKindOf, which is CObject‘s member. We can use these functions, because the class “Shape” is derived from CObject. IsKindOf function tells whether the instance of class is particular class type or not. We need to pass pointer to CRuntimeClass, which gets by calling RUNTIME_CLASS macro.

When we compile this code, the code will not compile and through the below message.

error C2065: 'classShape' : undeclared identifier

This is because, we have to enable Run-time class information. To enable this, we need to add DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros to our class. Remember that, DECLARE_DYNAMIC should goes into class declaration file (.h file) and IMPLEMENT_DYNAMIC should goes into class implementation file (.cpp file).

After adding these, the Shape class files looks like below:

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

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

	DECLARE_DYNAMIC(Shape);
};

And the implementation file:

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

using namespace std;

IMPLEMENT_DYNAMIC(Shape, CObject);

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

// -- main()
//
void main()
{
	Shape shape;

	if ( shape.IsKindOf(RUNTIME_CLASS(Shape)) == TRUE )
	{
		cout << "This is of type Shape." << endl;
	}
}

After these changes, compile the program: Ignore any warnings while compiling.

cl Shape.cpp /EHsc

Run the Shape.exe from command prompt and notice that it displays the string “This is of type Shape.”, which is what we expecting.

We will discuss about other features, in the next article.

**

MFC – CObject class – An Introduction – Enable Run-time class information

2 thoughts on “MFC – CObject class – An Introduction – Enable Run-time class information

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top