Click here to Skip to main content
Licence CPOL
First Posted 20 Feb 2010
Views 6,651
Downloads 29
Bookmarked 3 times

Emulation of CRuntimeClass for non-CObject derived classes

By | 20 Feb 2010 | Article
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.

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.

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).

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:

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)

About the Author

Rishat Bekmukhamedov

Software Developer (Senior)
Elstab
Uzbekistan Uzbekistan

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDerived class name for non-CObject derived classes Pinmembergeoyar7:29 23 Feb '10  
GeneralRe: Derived class name for non-CObject derived classes PinmemberRishat Bekmukhamedov19:26 23 Feb '10  
GeneralRe: Derived class name for non-CObject derived classes Pinmembergeoyar4:31 25 Feb '10  
GeneralRe: Derived class name for non-CObject derived classes PinmemberRishat Bekmukhamedov2:02 26 Feb '10  
GeneralSome Explaination PinmemberJohnWallis4210:11 21 Feb '10  
GeneralRe: Some Explaination [modified] PinmemberRishat Bekmukhamedov16:41 21 Feb '10  
GeneralRe: Some Explaination PinmemberJohnWallis4217:11 21 Feb '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 21 Feb 2010
Article Copyright 2010 by Rishat Bekmukhamedov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid