Object in COM and object in C ++

Source: Internet
Author: User
Object in COM and object in C ++ Author: Lou Guofu Release Date: 2001/02/07
 
Abstr:
After learning the COM technology for a while, I always think that some concepts in COM are relatively difficult. For example, the object concept is different from the object concept in C ++. This article briefly analyzes this concept, for beginners.

Body:  


Object in COM and object in C ++

After learning the COM technology for a while, I always think that some concepts in COM are difficult. For example, the object concept is different from the object concept in C ++. The following is a simple analysis, for beginners.
Generally, objects in COM/DCOM are called Windows objects. They are different from C ++ objects mainly because they are in C ++, we can only use to exist and execute C ++ objects in our own application (exe) or DLL. On the other hand, when using a Windows Object, you can ignore its existence and execution, either in your own EXE, or in a DLL, or in the local machine, it can also be on any host in the network. Therefore, Windows has better functions. There are several key differences between Windows objects and C ++ objects that affect programming implementation:
· Object class definition.
· Object Instantiation.
· Object reference.
· Object destruction.

Definition of object classes

In C ++, we use the class keyword to define the class, that is, a user-defined type is generated. Member Data and member functions can be private, protected, and public, and the C ++ class can inherit from another class and be inherited by another class, it can obtain all the features (data and member functions) of the base class, and has the ability to surpass and expand the selection items of the base class. Windows objects are defined in the form of interfaces supported by them. All windows objects must support an interface called iunknown. Only when an object supports this interface can it be called a windows object. The object user knows the other interfaces supported by the object through the iunknown member function.

Object Instantiation

C ++ objects can be instantiated in multiple ways, such as declaring a variable of the object type on the stack, declaring a full variable, or using the new operator for this type. However, no matter which technology is actually used, C ++ will eventually call the object's constructor.
Similarly, there are many ways to instantiate windows objects. In some cases, you can call a function to instance objects. In other cases, you cannot directly instance objects, but you can get a pointer to an object with other content already created. This section describes the most common technology, that is, using a tool called class factory object (class factory object) to instance windows objects, much like what the new operator has done to C ++ objects, A class factory object represents a identifier of a specific class, obtained through a specific ole2 or COM/DCOM function, and supports an interface named iclassfactory. The iclassfactory Interface contains a member function named createinstance, which can be passed with the identifier of the interface we want on the object. Iclassfactory: createinstance and new are logically equal.

Object Reference

A c ++ object can be referenced by an object variable, Object Reference (a specific type in C ++), or pointer. Because the object is usually local (in the user's exe or DLL), its instance can exist anywhere with the process space. With any variable, the user has access to any public, proprietary, or protected member of the object when the user and the object are friends. However, Windows objects are usually referenced through an interface pointer instead of an object pointer. This means that through an existing interface pointer, users can only access member functions in this interface. The user can never get a pointer to the complete object (because there is no definition of the complete object), so there is no access to the data staff, there is no concept of "friends.
Through the iunknown interface, you can reach another interface supported by this object, but this means that different pointers of the same object are obtained. Each interface pointer points to the function table in the object, and each function table only contains the member functions of a specific interface.
When you have an interface pointer of an object, you can call a member function of the interface, just like calling a member function of a C ++ object through a pointer:
Pisomeinterface-> memberfunction ([parameters]);

Object destruction

In C ++, the delete operation can be called on an object pointer to delete objects created through new. Objects that appear with stack variables can be automatically released after the stack is reset before being returned from a function. In the above two cases, the storage space occupied by the object is released, and the destructor of the object is called.
However, a function that releases a Windows Object and calls its destructor is a member function called release. This function is a member of iunknown, so it exists in each interface of the Windows Object. Release is not as crude as the delete operator. It involves the concept of "reference count". When release is called, the object may not be deleted. Internally, the object maintains a count on the number of references in any of its interfaces. When an interface pointer is created, a reference count is added, while release reduces the count. When the count is reduced to zero, the object releases itself and calls its own elimination function.

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.