What is the interface of C + + COM programming? _c language

Source: Internet
Author: User
Tags inheritance

What is an interface?

When it comes to COM, you have to say interface; In the process of COM development, it can be said that I have been dealing with a variety of interfaces. What's the interface? For COM, an interface is a memory structure that contains an array of function pointers, each containing an address of a function implemented by the component, so for COM, the interface is such a memory structure that other things are implementation details that some COM doesn't care about.

In C + +, you can use an abstract base class to implement a COM interface. Because a COM component can support any number of interfaces, for a component, you can use multiple inheritance of an abstract base class to implement it.

Benefits of Interfaces

interface provides a connection between two different objects. For customers, a component is an interface set. Customers can only interact with COM components through interfaces. And on the whole, customers have little knowledge of a component; Even at some point, customers do not even have to know all the interfaces provided by a component, as you do with Windows shell development, many times you cannot know all the interfaces for a component that it provides. Interfaces are the most important for an application. The component itself is nothing more than an implementation detail of the interface.

In actual development, you do not need to pay attention to the implementation details of the component, you are facing interface, interface work. Even if the developer of the component replaces the implementation of the component, and the interface does not change, your program does not need to change. Interface, just like a standard, let's follow this standard. One of the previous projects was to replace the implementation layer of a component, and for interfaces, no changes were required.

Simple to implement

Use a simple example to understand the interface:

Copy Code code as follows:

/*
* * Filename:simpleinterfacedemo
* * author:jelly Young
* * DATE:2013/12/11
* * Description:more information, http://www.jb51.net
*/
#include <iostream>
#include <combaseapi.h>
using namespace Std;
Interface IExample1
{
virtual void __stdcall Fx1 () = 0;
virtual void __stdcall Fx2 () = 0;
};
Interface IExample2
{
virtual void __stdcall Fy1 () = 0;
virtual void __stdcall Fy2 () = 0;
};
Implementation
Class Cimplementation:public IExample1, public IExample2
{
Public
Implementation IExample1
void __stdcall Fx1 () {cout<< "cimplementation::fx1" <<endl;}
void __stdcall Fx2 () {cout<< "cimplementation::fx2" <<endl;}
Implementation IExample2
void __stdcall Fy1 () {cout<< "cimplementation::fy1" <<endl;}
void __stdcall Fy2 () {cout<< "Cimplementation::fy2" <<endl;}
};
Client
int main ()
{
cout<< "Create An instance of the component." <<endl;
Cimplementation *pcimplementation = new Cimplementation;
Get the IExample1 pointer
IExample1 *piexample1 = pcimplementation;
Use the IExample1 interface
PIEXAMPLE1-&GT;FX1 ();
PIEXAMPLE1-&GT;FX2 ();
Get the IExample2 pointer
IExample2 *piexample2 = pcimplementation;
Use the IExample2 pointer
Use the IExample2 interface
Piexample2->fy1 ();
Piexample2->fy2 ();
Destroy the component
if (pcimplementation!= NULL)
{
Delete pcimplementation;
Pcimplementation = NULL;
PIExample1 = NULL;
PIExample2 = NULL;
}
}

In the example above, the client communicates with the component through two interfaces PIExample1 and PIExample2. When declaring an interface, two pure abstract base classes IX and IY are used. The key to summarizing the above code is:

The 1.COM interface is implemented in C + + with pure abstract base classes.
2. A COM component can provide multiple interfaces;
3. A C + + class can use multiple inheritance to implement a component that can provide multiple interfaces.

Detail analysis

Interface, where did this stuff come from? You'd be curious, wouldn't you be curious to fall out of your chin? C + + also have interface keywords? Yes, this keyword is defined in the Combaseapi.h header file and is defined as follows:

Copy Code code as follows:

#define __STRUCT__ STRUCT
#define INTERFACE __struct__

To put it bluntly, it is a structure defined by the keyword struct of C + +. What are the benefits of using the struct definition? First you need to figure out the difference between struct and class. Learn Java and C # are aware, because the interface is defined to allow customers to invoke, so in the interface does not need private and protected, if you use class, and must also use the Public keyword to emphasize the interface's common properties, The struct default is the public property, which eliminates the hassle of adding the common keyword.

What is __stdcall? __stdcall is a keyword used to modify a function, mainly by agreeing on two things:

1. The parameter passes the order, the __stdcall indicates the parameter from right to left presses into the stack;
2. The call stack is cleaned by WHO (calling a function or called function), and __stdcall represents modifying the stack by the called function.

The interface is implemented by pure virtual functions, why is this? And the polymorphic, this is a long story, and I'll summarize it in my next blog post.

Summarize

Here, the basic knowledge of the interface is a literacy summary, and these simple knowledge points are often encountered in future development, here to master these things, and so on later development will feel very relaxed. I hope you can learn a certain knowledge from this blog, but also hope that you have some pertinent suggestions to my blog.

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.