Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninjas!! Welcome to another article on Advanced Template Library. Today we will learn about Multiple Dual Interfaces in Advanced Template Library (ATL). The Advanced Template Library comprises all template-based C++ classes. It is helpful in creating small Component Object Model objects. The Component Object Model is a platform-independent object-oriented system used for developing binary software components.
Dual Interface allow users to access the methods of the class in two different ways. The first way is using dispinterface methods. The second way is using VTABLE (virtual table). The dispinterface is used when the application exposes its existing non-VTABLE functions through Automation.
Without further ado, let's get started.
Multiple Dual Interfaces
ATL comes with a feature of Multiple Dual Interfaces. The Multiple Dual Interfaces allow users to have the flexibility of VTABLE and dynamic binding. This makes the availability of classes to the scripting language and C++. The scripting language is a programming language where scripts are written for a runtime environment. The use of multiple dual interfaces ensures that there is less code complexity and no loss of functions.
Exposing a Single IDispatch Interface
ATL provides the method to expose multiple dual interferences on a single object. This can be achieved by deriving the multiple specializations of IDispatchImpl. This class contains the static members of type CComTypeInfoHolder. This static member manages the information type of dual interface. ATL also allows multiple queries for the IDispatch interface. In such cases, you also need to use the COM_INTERFACE_ENTRY2 macro. It specifies the base class of the IDispatch.
Example:
COM_INTERFACE_ENTRY2(IDispatch, IMyDualInterface)
You can also try this code with Online C++ Compiler
Here only one IDispatch interface is exposed. In such cases, you can only access the objects of your class.
Combining Multiple Dual Interfaces in a Single Implementation of IDispatch
ATL doesn't have the feature to combine Multiple Dual Interfaces in a Single Implementation of IDispatch. You can manually implement such features. The following are the different ways to combine multiple dual interfaces into a single implementation:
Creating a templated class that contains all the separate IDispatch interfaces.
Creating a new object to carry out the QueryInterface function. QueryInterface() is used to obtain an interface pointer of an another class from the exiting interface pointer.
Using the nested objects with typeinfo. The typeinfo is a header file which defines several types associated with the type identification operator, which yields information about both static and dynamic types.
The above approaches are not practically feasible. This might result in code complexity and namespace collisions.
Nonextensible Attribute
The nonextensible attribute gives information to client languages (like Visual Basic) that can be used to enable full code verification during compile time. This attribute is helpful when the dual interface is not enabled during runtime. This attribute prevents bugs in the client code during runtime.
Syntax:
[nonextensible]
You can also try this code with Online C++ Compiler
In the above code snippet, we had created an interface with the name CodingNinjas. It is a dual and nonextensible attribute. It is helpful when the dual interface is not enabled during the runtime of the code.
Frequently Asked Questions
What is an Object Model?
The Object Model visualizes the software elements as objects.
Is it good to combine Multiple Dual Interfaces in a Single Implementation of IDispatch?
No, combining Multiple Dual Interfaces into a Single Implementation of IDispatch would increase the complexity of the code.
What is a Virtual Table?
A virtual table is a lookup table of functions used to solve function calls in dynamic binding.
Conclusion
In this article, we have discussed the details of Multiple Dual Interfaces and nonextensible Attributes in ATL.