Click here to Skip to main content
15,897,187 members
Articles / Programming Languages / C++

Emulation of CRuntimeClass for non-CObject derived classes

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
20 Feb 2010CPOL 22.7K   80   4   7
MACROs having advantage of CRuntimeClass for non-CObject derived classes.

Introduction

Let's assume you decided to create a hierarchy of non-CObject derived classes using the CRuntimeClass of MFC libraries (and DECLARE_DYNAMIC IMPLEMENT_DYNAMIC DECLARE_DYNCREATE IMPLEMENT_DYNCREATE as well). It is implied that your base class is named like CXxx (C + class prefix) and all your child classes have names according to the rule: C+ class prefix + kind_name (for example, CXxxCool, CXxxAdvanced, CXxxSuper etc.). You need to have a static function Create(string kind_name) to create objects at runtime and a function GetKind returning a string with a 'kind_name' of the object.

Background

Unlike CRuntimeClass (actually it is a structure), our 'CKindOf...' class exists as MACROs DECLARE_KIND_OF(class_name) and IMPLIMENT_KIND_OF(class_name), and every generated 'KindOf' class corresponds with a given class hierarchy.

C++
DECLARE_KIND_OF(Xyz)

IMPLIMENT_KIND_OF(Xyz)

Using the code

The base class of your hierarchy should use the DECLARE_BASE_DYNCLASS(class_name) and IMPLIMENT_BASE_DYNCLASS(class_name) MACROs.

C++
class CXyz
{

public:
    CXyz();
DECLARE_BASE_DYNCLASS(Xyz)
};

IMPLIMENT_BASE_DYNCLASS(Xyz)
CXyz::CXyz()
{

}

And every child class should use DECLARE_CHILD_DYNCLASS(class_name,kind_name) and IMPLIMENT_CHILD_DYNCLASS(class_name,kind_name,parent_name).

C++
class CXyzCool:public CXyz
{
  DECLARE_CHILD_DYNCLASS(Xyz,Cool)
};

IMPLIMENT_CHILD_DYNCLASS(Xyz,Cool,Base)

class CXyzAdv:public CXyzCool
{
  DECLARE_CHILD_DYNCLASS(Xyz,Adv)
};

IMPLIMENT_CHILD_DYNCLASS(Xyz,Adv,Cool)

If your child class is derived directly from the base class (the second level in the class hierarchy), use the 'Base' keyword as the third argument.

IMPLIMENT_CHILD_DYNCLASS(Xyz,Cool,Base)

And the prefix of the base class otherwise:

C++
IMPLIMENT_CHILD_DYNCLASS(Xyz,Adv,Cool)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Elstab
Uzbekistan Uzbekistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDerived class name for non-CObject derived classes Pin
geoyar23-Feb-10 7:29
professionalgeoyar23-Feb-10 7:29 
GeneralRe: Derived class name for non-CObject derived classes Pin
cyberanarchist23-Feb-10 19:26
cyberanarchist23-Feb-10 19:26 
GeneralRe: Derived class name for non-CObject derived classes Pin
geoyar25-Feb-10 4:31
professionalgeoyar25-Feb-10 4:31 
GeneralRe: Derived class name for non-CObject derived classes Pin
cyberanarchist26-Feb-10 2:02
cyberanarchist26-Feb-10 2:02 
GeneralSome Explaination Pin
JohnWallis4221-Feb-10 10:11
JohnWallis4221-Feb-10 10:11 
GeneralRe: Some Explaination [modified] Pin
cyberanarchist21-Feb-10 16:41
cyberanarchist21-Feb-10 16:41 
Here is a part of MFC documentation dedicated to this class. I think I can't explain what CRuntimeClass is better than they Wink | ;) :

"CRuntimeClass

Each class derived from CObject is associated with a CRuntimeClass structure that you can use to obtain information about an object or its base class at run time. The ability to determine the class of an object at run time is useful when extra type checking of function arguments is needed, or when you must write special-purpose code based on the class of an object. Run-time class information is not supported directly by the C++ language.
The structure has the following members:
...~...
CObject* CreateObject( ); Classes derived from CObject can support dynamic creation, which is the ability to create an object of a specified class at run time. Document, view, and frame classes, for example, should support dynamic creation. The CreateObject member function can be used to implement this function and create objects for these classes during run time.
BOOL IsDerivedFrom( const CRuntimeClass* pBaseClass) const; Returns TRUE if the class of the class member calling IsDerivedFrom is derived from the base class whose CRuntimeClass structure is given as a parameter. IsDerivedFrom walks from the member's class up the chain of derived classes all the way to the top and returns FALSE only if no match is found for the base class.

Note To use the CRuntimeClass structure, you must include the IMPLEMENT_DYNAMIC, IMPLEMENT_DYNCREATE, or IMPLEMENT_SERIAL macro in the implementation of the class for which you want to retrieve run-time object information."
modified on Sunday, February 21, 2010 10:58 PM

GeneralRe: Some Explaination Pin
JohnWallis4221-Feb-10 17:11
JohnWallis4221-Feb-10 17:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.