1. The action component of the interface can be deleted from the application and replaced by another component, as long as the new component supports the same interface. A single component does not play a decisive role. On the contrary, interfaces used to connect components play a decisive role on application relatives. The biggest advantage of using components to form an application is that the application structure can be reused. Interfaces can protect the system from external changes, and allow customers to process different components in the same way. 2. Implement the COM interface class IX // first
Interface {public: Virtual void fx1 ()
= 0; virtual void fx2 ()
= 0 ;};
Class Iy // second
Interface {public: Virtual void fy1 ()
= 0; virtual void FY2 ()
= 0 ;};
Class CA: Public IX, public Iy {public: // implement interface IX virtual void fx1 () {cout <"fx1 ()" <Endl;} virtual void fx2 () {cout <"fx2 ()" <Endl ;}// implement interface Iy virtual void fy1 () {cout <"fy1 ()" <Endl ;} virtual void FY2 () {cout <"FY2 ()" <Endl ;}}; IX and Iy are pure abstract base classes used to implement interfaces. Component Ca inherits the IX and Iy interfaces. The abstract base class specifies which functions should be provided by the derived class. The derived classes implement these functions. The Inheritance of pure virtual classes is called interface inheritance. The COM interface must inherit from the iunknown interface. In objbase. H, define # define interface struct. Therefore, use struc without having to keep public more concise. Summary:
- The COM interface is implemented using a pure abstract base class in C ++.
- A com component can provide multiple interfaces
- A c ++ class can use multiple inheritance to implement a component that can provide multiple interfaces.
(1) _ stdcall call _ stdcall is the default call method of the PASCAL program. The parameter uses the pressure stack method from right to left, and the called function itself clears the stack before returning. Win32 APIs all use the _ stdcall call method. This macro definition illustrates the problem: # define winapi _ stdcall is compiled in C mode, where the _ stdcall call convention underscores the name of the output function, add the "@" symbol and the number of bytes of the parameter, for example, _ functionname @ NNN. (2) _ cdecl call _ cdecl is the default call Method for C/C ++. The parameter uses the stack pressure mode from right to left, the memory stack of the transfer parameter is maintained by the caller. _ Functions agreed by cedcl can only be called by C/C ++. Each function that calls it contains code for clearing the stack, therefore, the size of the executable file is larger than that of the _ stdcall function. Since the parameter memory stack of the _ cdecl call method is maintained by the caller, the variable-length parameter function can (or can only) use this call convention. Because visual
C ++ uses the _ cdecl call method by default. Therefore, when calling a DLL in VC, you should use the _ stdcall call convention. In C compilation mode, __cdecl indicates that only the name of the output function is underlined, such as _ functionname. 3. Implementation Details
- Class is not a component
- Interfaces are not always inherited.
You can use a class to implement several different interfaces. You can also use a single class to implement each interface.
- Multi-interface and multi-Inheritance
An interface is a function set, a component is an interface set, and a system is a set of components.
4. The virtual function table behind the interface defines the structure of a memory block when defining an abstract base class. All implementations of the pure abstract base class are memory block interfaces IX {virtual void _ stdcall fx1 () with the same basic structure ()
= 0; virtual void _ stdcall fx2 ()
= 0; virtual void _ stdcall FX3 ()
= 0; virtual void _ stdcall fx4 ()
= 0 ;}; define a pure abstract base class that defines the corresponding memory structure. Is allocated only when the abstract base class exists in the derived class. When a derived class inherits an abstract base class, it inherits this memory structure: the memory structure defined by an abstract base class is divided into two parts. The right side is the virtual function table (vtbl), which contains a group of pointers pointing to the virtual function implementation. The first item in vtbl is the address of the fx1 function implemented in the derived class. The left side is a pointer to vtbl, And the pointer to the abstract base class points to the vtbl pointer. Vtbl pointer and instance data class CA: Public IX {public: // implement interface IX virtual void _ stdcall fx1 () {cout <"CA: fx1 () "<Endl;} virtual void _ stdcall fx2 () {cout <m_fx2 <Endl;} virtual void _ stdcall FX3 () {cout <m_fx3 <Endl;} virtual void _ stdcall fx4 () {cout <m_fx4 <Endl ;}// Ca (double D ): m_fx2 (D * D), m_fx3 (D * D), m_fx4 (D * D) {}// interface data double m_fx2; double m_fx3; double m_fx4 ;};