Chapter 1 COM is a better C ++

xiaoxiao2021-03-06  59

Software distribution and C

Distribute in the form of source code:

Question 1: Each executable will contain the code of the class library, wasting disk space, if users run several applications containing the library at the same time, waste virtual memory.

Question 2: Once the library vendor discovers the defect, there is no way to replace some implementation code

Dynamic links with C

The introduction library does not include the actual code, generated by the linker, which contains some references, pointing to the DLL file name and the resulting symbol name.

With the introduction library, the machine code is only reserved on the hard disk.

C portability

Because there is a name mangling, a compiler's customer cannot be successfully linked to the introduction of the B compiler.

EXTERN "C" is classic technology that eliminates the name adaptation phenomenon, but it cannot be used for member functions.

Do some articles on the customer's linker, use DEF (Module Definition File): It allows the extraction symbol to be posed to different introduction symbols, librarkers can produce specialized-customized introduction libraries for different compilers.

A function generated by the compiler, it throws an exception that cannot be captured by a client generated by the B compiler.

Packaging and C

C supports syntax encapsulation through private and public keywords, but does not define encapsulation at binary hierarchies.

This is because the C compilation model requires the customer's compiler to access all information related to the object's memory layout, so that the class's instance is constructed, or the class's non-virtual member function is called. These information includes the private member of the object and the public The size and order of members.

Separate the interface from the implementation

Interface classes should only describe the appearance of the underlying data type that the implementator wants customers know.

Use the handle class as an interface.

Disadvantages: Each new method should add two functions calls, easy to go wrong, and overhead

Abstract base class as a binary interface

Suppose 1: Compound types of performances often remain unchanged for different compilers

Suppose 2: All compilers are forced to use the same order to transfer function parameters and the cleaning of the stack is also performed in a statistics table.

Suppose 3: A given platform C compiler implements the same virtual function call mechanism

Abstract base class requirements:

Method is Virtual

DLL leads out a full-class function, which calls New on behalf of the customer, using Extern "C"

Add a DELETE virtual method, instead of the false prevention function (because it is related to the compiler in the location of VTAL)

The virtual function of the interface class is always used indirectly by saving the function pointer in the VTBL, and the client does not need to link the symbolic names of these functions at the time of development.

Runtime polymorphism

The user can dynamically load the DLL, reduce the work initialization of address space, and if the object implements code, the DLL will not be loaded.

Allow customers to dynamically make choices between different implementations

Object scalability

If you add new methods: Old customers can work with new objects, but new customers get old objects that cannot call new methods

Allow the implementation of the class to reveal more interfaces:

An interface inherits another associated interface that enables the class inherits multiple unrelated interfaces.

Customers use RTTI to ensure that the current object supports the functionality of the customer request

But RTTI is related to the compiler, for this purpose

From the exposure of each interface, a universal method Dynamic_cast () makes it works with Dynamic_CAST.

Resource management

Reference Counting Use Principles:

DuplicatePointer () is called when the interface pointer is copied.

Call DestroyPointer () when it is no longer used

The pointer is seen as an entity with independent life cycle, so can the customer do not need to link which pointer and which object?

Allow objects to manage your life cycle

转载请注明原文地址:https://www.9cbs.com/read-117217.html

New Post(0)