C ++ is a powerful C language-based computer programming language. It not only supports a variety of C language functions, but also supports various programming styles including object-oriented. Today, we will give you a detailed introduction to some of the application tips for Dynamic Object creation in C ++.
- C ++ delete usage
- Different methods of using C ++ destructor
- C ++ implicit Conversions
- Several different conversion methods in C ++ explicit conversion
- Overview of common C ++ Class Libraries
Native C ++ does not support dynamically creating objects based on class names. For example, it reads class names from a text file and constructs an object. the main reason is that there is no rich dynamic metadata and there is no single class library. However, it can be implemented using several technologies. If an object that implements an interface is generated according to the configuration file runtime like Spring, there are at least three methods in Windows:
1. LoadLibrary + GetProcAdress. The DLL and Proc names can be dynamically passed in without much explanation.
2. COM. Call GetClassObject to obtain the IClassFactory Interface Based on the dynamically obtained CLSID, and then CreateInstance. Or call CoCreateInstance/CoCreateInstanceEx to dynamically generate CoClass.
3. MFC dynamic creation.
Native C ++ cannot compile code and generate objects at runtime, so dynamic JIT generation cannot be done in C ++. In MFC, you can refer to and summarize as follows:
The most easy-to-use solution is to dynamically create objects in C ++ of MFC. I checked the implementation of RTCI in MFC and summarized it as follows:
The DECLARE _ DYNCREATE (class_name) macro is displayed in the following format:
Assume that class_name is "CMyClass"
- public:
- static CRuntimeClass classCMyClass;
- virtual CRuntimeClass* GetRuntimeClass() const;
- static CObject* CreateObject();
These lines will be added to the declaration of the CMyClass class.
IMPAEMENT_DYNCREATE (classname, base_classname) macro definition is complex. After the macro is expanded, it looks like the following:
- AFX_DATADEF CRuntimeClass CMyClass::classCMyClass = {
- "CMyClass",sizeof(CMyClass),0xFFFF,NULL,RUNTIME_CLASS(CObject),NULL};
- static const AFX_CLASSSINIT _init_CMyClass(&CMyClass::classCMyClass);
- CRuntimeClass* CMyClass::GetRuntimeClass() const
- {
- Return & CMyClass::classCMyClass;
- }
- CObject* PASCAL CMyClass::CreateObject()
- {
- return new CMyClass;
- }
This macro does the following three tasks:
1. initialize the member variable classCMyClass of the CRuntimeClass type
2. Create a static AFX_CLASSINIT structure. The structure is as follows:
- Struct AFX_CLASSINIT
- {AFX_CLASSINIT(CRuntimeClass* pNewClass);};
This step is mainly used to add CMyClass: classCMyClass to an internal linked list of MFC.
3. Overwrite GetRuntimeClass () to return the address of the member variable classCMyClass.
- The RUNTIME_CLASS macro is expanded as follows:
- (& Class_name: class # class_name)
C ++ calls the CRuntime: CreateObject () method when dynamically creating an object.
This method actually calls a member pointer in the CRuntime, which points to the CMyClass: CreateObject () method.
As shown in the preceding figure, the RuntimeClass macro accepts strings as parameters. However, you still need to define the object types to be dynamically created during compilation. The preceding example is CMyClass. This feature of MFC allows you to read text from the configuration file in theory, and then dynamically create Objects Based on the Type C ++ specified by the text, but this type must exist during compilation, it cannot be as unlimited as dynamic languages. In addition, CMyClass must inherit from CObject.