Chapter 1 of "com Essence Theory" (4)

Source: Internet
Author: User

Class iextensibleobject
{
Virtual void * dynamic_cast (const char * psztype) = 0;
Virtual void duplicatepointer (void) = 0;
Virtual void destroypointer (void) = 0;
};

 

The base class interface is abstracted into this form. dynamic_cast () is used to identify the runtime type. duplicatepointer () implements reference counting and destroypointer () releases the object.

 

Solved problems:

1. c ++'s dynamic_cast is related to the compiler. Therefore, a well-known method is exposed from each interface, and this method is used to achieve the equivalent function of dynamic_cast semantics.

2. When multiple pointers direct to the same object, it is easy to call the delete () function multiple times. Therefore, the reference counting mechanism is used to ensure that delete () is called only once.

 

Obviously, these three methods are required by every interface, so they are abstracted into a base class.

Class ifaststring: Public iextensibleobject
{
Public:
Virtual int length (void) const = 0;
Virtual int find (const char * psz) const = 0;
};

 

 

Combine the first three parts (the first three partsArticleAfter this step is completed, the class can be made into reusable binary components.

 

Train of Thought:

Step 1: publish a class in the form of a dynamic library, physically separating the class from the customer.

Step 2: the interface and implementation are separated so that the customer does not need to care about the memory layout of the class.

Step 3: Use the abstract base class as the interface definition method to build a binary firewall in the form of vptr and vtbl.

Step 4: Use loadlibrary and getprocaddress to dynamically select different implementations of the same interface during runtime.

Step 5: Use a structure similar to rtti to dynamically query the object at runtime to determine whether the object has implemented the specified interface.

Step 6: Use the reference count to ensure that a single object is referenced by multiple pointers without repeated deletion.

This is the com model.

 

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.