Table of contents
1.
Introduction
2.
What are Tear Off Interfaces Classes
3.
Advantages and Uses 
4.
CComTearOffObject 
4.1.
CComTearOffObject::AddRef
4.2.
CComTearOffObject::CComTearOffObject
4.3.
CComTearOffObject::~CComTearOffObject
4.4.
CComTearOffObject::QueryInterface
4.5.
CComTearOffObject::Release
5.
CComCachedTearOffObject Class
5.1.
CComCachedTearOffObject::AddRef
5.2.
CComCachedTearOffObject::CComCachedTearOffObject
5.3.
CComCachedTearOffObject::~CComCachedTearOffObject
5.4.
CComCachedTearOffObject::FinalConstruct
5.5.
CComCachedTearOffObject::FinalRelease
5.6.
CComCachedTearOffObject::m_contained
5.7.
CComCachedTearOffObject::QueryInterface
5.8.
CComCachedTearOffObject::Release
6.
Frequently Asked Questions 
6.1.
What are Tear Off Interfaces Classes?
6.2.
Name two Tear Off Interfaces Classes.
6.3.
Differentiate between a Constructor and a Destructor.
6.4.
What is an IUnknown Interface?
6.5.
What is the need for Active Template Library?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Tear Off Interfaces Classes

Author Geetika Dua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Are you aware of the Active Template Library? Do you want to learn more about it? Do you understand the concept of Tear Off Interfaces Classes? You have come to the right place. This blog will study the Tear Off Interfaces Classes in the Active Template Library.

Tear off interface classes

Let us understand Tear Off Interfaces Classes.

What are Tear Off Interfaces Classes

The first question that may arise is, what exactly are Tear-Off Interfaces Classes?

A tear-off interface is an interface we want to expose on demand but not inherit from the main class. Tear-Off Interfaces Classes fulfill this requirement.

The following classes support tear-off interfaces:

  • CComTearOffObject 
  • CComCachedTearOffObject 

Advantages and Uses 

There are several advantages of using Active Template Library.

We can create small, fast Component Object Model objects. It supports stock implementations, dual interfaces, connection points, and tear-off interfaces.

There are performance and program architecture reasons to prefer tear-off interfaces. The critical thing is that when asked for a service, the object hands you back a different object that implements that service rather than implementing the service itself. This makes the implementation smoother and quicker.

CComTearOffObject 

This class implements the Tear-Off Classes Interfaces. The syntax of this class is as follows-

template<class Base>
class CComTearOffObject: public Base


CComTearOffObject implements a tear-off interface as a separate object that is instantiated only when that interface is queried for. When the reference count becomes zero, it gets deleted.

CComTearOffObject::AddRef

It increments the reference count of the CComTearOffObject object by one.

The syntax is as follows-

STDMETHOD_(ULONG, AddRef)();

CComTearOffObject::CComTearOffObject

It is the Constructor for this class. We use Constructor to create an instance of the class. The syntax is as follows-

CComTearOffObject(void* pv);

CComTearOffObject::~CComTearOffObject

It is the destructor and destroys the object. The syntax is as follows-

~CComTearOffObject();

CComTearOffObject::QueryInterface

It queries the pointer when there is a request. The syntax for retrieving the query to a pointer is-

STDMETHOD(QueryInterface)(REFIID iid, void** ppvObject);

CComTearOffObject::Release

The job of this method is to decrement the count by one. If the reference count becomes zero, it deletes the object. The syntax is as follows-

STDMETHOD_ULONG Release();

CComCachedTearOffObject Class

This class has to implement the IUnknown for a tear-off interface. IUnknown interface allows us to get pointers to other interfaces on a given object.

template
<class contained>
class CComCachedTearOffObject: public
    IUnknown,
public CComObjectRootEx<contained
::_ThreadModel::ThreadModelNoCS>


Let us study the methods-

CComCachedTearOffObject::AddRef

This method increments the count of the object by 1. The syntax of the same is -

STDMETHOD_(ULONG, AddRef)();

CComCachedTearOffObject::CComCachedTearOffObject

It is the Constructor for this class. Constructor is used to creating an instance of the class. The syntax is as follows-

CComCachedTearOffObject(void* pv);

CComCachedTearOffObject::~CComCachedTearOffObject

It is the destructor and destroys the object. The syntax is as follows-

~CComCachedTearOffObject();

CComCachedTearOffObject::FinalConstruct

This method calls m_contained::FinalConstruct to create m_contained. The CComContainedObject< contained> is used to access the interface implemented by the tear-off class. The syntax is as follows-

HRESULT FinalConstruct();

CComCachedTearOffObject::FinalRelease

This method calls m_contained::FinalRelease and frees m_contained. m_contained is the CComContainedObject< contained> object. The syntax is written below-

void FinalRelease();

CComCachedTearOffObject::m_contained

This is a CComContainedObject object derived from your tear-off class. The syntax is as follows-

CcomContainedObject <contained> m_contained;

CComCachedTearOffObject::QueryInterface

It retrieves a pointer to the requested interface. The syntax is-

STDMETHOD(QueryInterface)(REFIID iid, void** ppvObject);


iid

The GUID of the interface is requested.

ppvObject

It is a pointer to the interface. It is identified by iid or NULL if the interface is not found.

CComCachedTearOffObject::Release

It decrements the reference count by one and, if the reference count is 0, deletes the CComCachedTearOffObject object. The syntax is-

STDMETHOD_(ULONG, Release)();

Frequently Asked Questions 

What are Tear Off Interfaces Classes?

A tear-off interface is an interface we want to expose on demand but not inherit from the main class.

Name two Tear Off Interfaces Classes.

Two Tear-Off Interfaces Classes are CComTearOffObject and CComCachedTearOffObject.

Differentiate between a Constructor and a Destructor.

We use a Constructor to create an instance of the class. The destructor destroys the object.

What is an IUnknown Interface?

IUnknown interface allows us to get pointers to other interfaces on a given object.

What is the need for Active Template Library?

The Active Template Library (ATL) is a set of template-based C++ classes developed by Microsoft, intended to simplify the programming of Component Object Model (COM) objects.

Conclusion

This blog has provided you with the concept of Tear Off Interfaces Classes. We hope that you understand this concept clearly. For more about Active Template Library, Refer to our other blogs-

  1. Object Safety Classes
  2. Site Information Classes
  3. Service Provider Support Classes
     

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass