Similar to the basic class baseclass of cobject, The runtimeclass of cruntimeclass and the related macro definition header files are as follows:
/// // Baseclass. h
# Ifndef baseclass_h
# Define baseclass_h
# Include <iostream>
Class baseclass;
Struct runtimeclass
{
Char * classname;
Baseclass * (* pcreatefn )();
Void * Createobject ();
Static runtimeclass * loadbyname (STD: String classname );
Static runtimeclass * pfirstclass;
Runtimeclass * pbaseclass;
Runtimeclass * pnextclass;
};
Struct class_list
{
Class_list (runtimeclass * pnewclass );
};
# Define runtime_class (classname )/
(& Classname: Class ## classname)
# Define declare_rtti (classname )/
Public :/
Static runtimeclass class ## classname ;/
Virtual runtimeclass * getruntimeclass () const ;/
# Define implement_rtti (classname, basename )/
Static char lpsz # classname [] = # classname ;/
Runtimeclass classname: Class ## classname =/
{Lpsz # classname, null, runtime_class (basename), null };/
Static class_list _ init _ # classname (& classname: Class ## classname );/
Runtimeclass * classname: getruntimeclass () const/
{Return & classname: Class ## classname ;}/
# Define declare_dyncreate (classname )/
Declare_rtti (classname )/
Static baseclass * Createobject ();/
# Define implement_dyncreate (classname, basename )/
Baseclass * classname: Createobject ()/
{Return New classname ;}/
Static char lpsz # classname [] = # classname ;/
Runtimeclass classname: Class ## classname =/
{Lpsz # classname, classname: Createobject, runtime_class (basename), null };/
Static class_list _ init _ # classname (& classname: Class ## classname );/
Runtimeclass * classname: getruntimeclass () const/
{Return & classname: Class ## classname ;}/
Class baseclass
{
Public:
Virtual runtimeclass * getruntimeclass () const
{
Return & baseclass: classbaseclass;
}
Virtual void printclassname () {STD: cout <"baseclass! /N ";}
Public:
Static runtimeclass classbaseclass;
};
# Endif
//////////////////////////////////////// /// // Baseclass. CPP
# Include "baseclass. H"
Runtimeclass * runtimeclass: pfirstclass = NULL;
Static char lpszbaseclass [] = "baseclass ";
Runtimeclass baseclass: classbaseclass = {lpszbaseclass, null, null };
Static class_list _ init_baseclass (& baseclass: classbaseclass );
Class_list: class_list (runtimeclass * pnewclass)
{
Pnewclass-> pnextclass = runtimeclass: pfirstclass;
Runtimeclass: pfirstclass = pnewclass;
}
Void * runtimeclass: Createobject ()
{
If (pcreatefn = NULL)
{
Return NULL;
}
Void * pobject = NULL;
Pobject = (* pcreatefn )();
Return pobject;
}
Runtimeclass * runtimeclass: loadbyname (STD: String classname)
{
Runtimeclass * Pclass;
For (Pclass = pfirstclass; Pclass! = NULL; Pclass = Pclass-> pnextclass)
{
If (strcmp (classname. c_str (), Pclass-> classname) = 0)
Return Pclass;
}
Return NULL;
}
//////////////////////////////////////// ///////////// Three test classes classa, classb, classas
# Pragma once
# Include "baseclass. H"
Class classa:
Public baseclass
{
Declare_dyncreate (classa)
Public:
Classa (void );
Public:
~ Classa (void );
Public:
Void printclassname ()
{
STD: cout <"classa! /N ";
}
Public:
Void dosomething ()
{
STD: cout <"dosomething/N ";
}
};
# Include "classa. H"
Implement_dyncreate (classa, baseclass)
Classa: classa (void)
{
}
Classa ::~ Classa (void)
{
}
/////////////////////////
# Pragma once
# Include "baseclass. H"
Class classb:
Public baseclass
{
Declare_dyncreate (classb)
Public:
Classb (void );
Public:
~ Classb (void );
Public:
Void printclassname ()
{
STD: cout <"classb/N ";
}
};
# Include "classb. H"
Implement_dyncreate (classb, baseclass)
Classb: classb (void)
{
}
Classb ::~ Classb (void)
{
}
////////////////////////////////////
# Pragma once
# Include "classa. H"
Class classas:
Public classa
{
Declare_dyncreate (classas)
Public:
Classas (void );
Public:
~ Classas (void );
Public:
Void printclassname ()
{
STD: cout <"classas/N ";
}
};
# Include "classas. H"
Implement_dyncreate (classas, classa)
Classas: classas (void)
{
}
Classas ::~ Classas (void)
{
}
//////////////////////////////////////// //// // Test
# Include "baseclass. H"
# Include <iostream>
# Include "classa. H"
Using namespace STD;
# Define dotest (classname, varname )/
Classname * # varname = new classname ();/
Varname-> dosomething ();/
Int _ tmain (INT argc, _ tchar * argv [])
{
Runtimeclass * PRC;
For (PRC = runtimeclass: pfirstclass; PRC! = NULL; PRC = PRC-> pnextclass)
{
Cout <PRC-> classname;
If (PrC-> pbaseclass! = NULL)
Cout <"--" <PRC-> pbaseclass-> classname <Endl;
Else
Cout <"-- null" <Endl;
}
Getchar ();
PRC = runtimeclass: loadbyname ("classas ");
Classa * pb = (classa *) PRC-> Createobject ();
Pb-> printclassname ();
Dotest (classa, CA)
Getchar ();
Return 0;
}