Smart pointers in ATL and VC + + (CComPtr and _com_ptr_t, respectively)

Source: Internet
Author: User

First, the concept of smart pointers

The smart pointer is a class, not a pointer , and the smart pointer automatically frees the system resources that are used by the contained pointer when it is no longer in use, rather than manually releasing it.

Principle: The smart pointer encapsulates the AddRef () function and the release () function, which contains pointers, and calls the release () function containing pointers in the destructor when the class is not needed to release the resource containing the pointer. In essence, the purpose of invoking the release () function with a pointer is to use the class's destructor .

Second, VC in the smart pointer: _com_ptr_t

_com_ptr_t is essentially a class template. You need to provide three parameters when using it: the name of the interface, the IID of the interface, and the CLSID of the implementation class. (in fact, similar to the parameters required to use CoCreateInstance)

1. Steps:

1. Define a smart pointer:_com_smatrtptr_typedef (imyinterface,__uuidof (IMyInterface));

Parameter 1: The name of the included interface;

Parameter 2: The GUID value of the contained interface.

The above macro will automatically generate a smart pointer called IMYINTERFACEPTR (a Class), with this class, we can define the class object to invoke the function within the interface.

2. Define a smart pointer object:

Imyinterfaceptr Pmyinterface;

3. Instantiate the class that implements the interface.

Method One: Pmyinterface.createinterface (__uuidof (MyInterface));//Suppose MyInterface is a class that implements the IMyInterface interface.

Method Two: Pmyinterface (__uuidof (MyInterface));//Because the CreateInstance () function is automatically called in the smart pointer constructor, the GUID of the implementation class is provided directly.

4. Call the interface pointer.

Pmyinterface->show ();

Note: When a smart pointer is refactored, the release () function of the interface is automatically called, which frees the class MyInterface resources, so no more calls to release (), and can be invoked if not at ease.

That is, pmyinterface.release ();//Note that the middle is a point operation symbol, not a.

2. Why the smart pointer can also be used with the. operator, but also with the operator:

The. operator is the smart pointer class that calls its own method, with the operator called the method that contains the interface, because the smart pointer class has a variable m_pinterface used to store the contained interface, essentially Pwiseptr.m_pinterface->method ().

Advantages of smart pointers:

1. When we call CreateInstance () to generate an interface implementation class object, the smart pointer is automatically called a _release () Private function, so that the previous interface unconditionally released (the number of references minus 1).

2. A pointer to a m_pinterface interface* type is stored within the smart pointer to store pointers to the contained interfaces. The pointer pointer invokes the interface function, which is essentially the result of the variable invocation.

Third, we just need to know how to use

1. The smart pointer does not release, when the program does not need the smart pointer Auto-destruction, the destruction of the included interface automatically reduce the number of references 1;

2. Create a smart pointer via macro _com_smatrtptr_typedef (Imyinterface,__uuidof (IMyInterface));

3.ADO, WORD, Excel and other programs provide smart pointers, don't be confused.

Reference documents:

1.https://www.cnblogs.com/coderlee/archive/2008/02/18/1071755.html

2.5996969

Smart pointers in ATL and VC + + (CComPtr and _com_ptr_t, respectively)

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.