Programmer with virtual functions, is sure to have a psychology that switch / case sentence model, because if there is too much case, at least the following shortcomings:
1) The code is too long, not suitable for viewing and management;
2) Switch / case is not flexible enough;
3) The expansion is not good enough, such as each of the addition of a CASE to change the code.
The MFC has a good framework that inherits from COBJECT and declares clarisms by declare_dynamic () and import_dynamic, which is dynamically created CruntimeObject will be added to the category record network. If this dynamic creation class can be created, you can create This type of object. The following code is to find a dynamic creation class corresponding to the class name in the Cateigree British. If you are found, create an object and return to the object pointer; otherwise return null.
CObject * getobjbyclassname (cstring strclassname)
{
CruntimeClass * PCLASS;
AFX_MODULE_STATE * PModuleState = AFXGETMODULESTATE ();
For (pclass = pmodules-> m_classlist; pclass! = null;
PCLASS = PCLASS-> m_pnextclass) {
IF (strclassname.compare (pclass-> m_lpszclassname == 0) {
Return (PCLASS-> CREATEOBJECT ());
}
}
Return NULL;
}
The returned pointer is a cobject * type, which can be converted to the type you need after getting it! (Pay attention to safety control)
Note:
1) Inherit from COBJECT
2) Declare_dynamic () and import_dynamic declaration
3) There must be a default constructor