Simulate MFC to implement the function of dynamically creating objects by name in C ++

Source: Internet
Author: User

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;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.