Application of Interface in Host C ++

zhaozj2021-02-16  52

Application of Interface in Host C

The hosted C is also C in the .NET environment, which is vc.net, here is not talking MFC7

In fact, another part of VC.NET is to run in the hosted environment. Although the interface of the interface

There are many, have to say that the word is started from COM. With the popular words of COM

More and more is used. The interface between C in COM is actually an abstract data definition of pure virtual functions.

E.g:

The definition of the interface in CoM:

Class iClassFactory: Public IUNKNOWN

{

Virual HRESULT _STDCALL CREATEINSTANCE (IUNKNOW * PUNKNOWNOUTER, Const IID & IID, Void ** PPV) = 0; // Note that it is pure

Virual HRESULT _STDCALL LOCKSERVER (BOOL Block) = 0; // Note that it is pure

}

The implementation of the interface in CoM:

Class CXXCom: Public iClassFactory

{

protected:

Ulong m_ref;

PUBLIC:

CXXCOM (Void);

~ Cxxcom (void);

HRESULT QueryInterface (Const IID & IID, VOID ** PPV);

Ulong addRef ();

Ulong release ();

HRESULT CREATINSTANCE (IUNKNOWN *, Const IID & IID, VOID ** PPV);

HRESULT LOCKSERVER (BOOL);

}

Of course, MFC does not do this, it will be done from ccmdtarget and use macros and a linked list. But the principle is the same.

Of course, I am talking about this purpose, I don't want to write a COM because it is not the purpose I wrote this article.

The reason why I have to write, this section is to indicate that the implementation of the interface in the unmanaged C is actually used in a class.

The function of pure virtuality is not true. It is not a real interface. Because there is no interface in the standard C . So this is

A changed manner. In the development to the hosted environment, several mainstream .NET languages ​​realize the language of the interface.

Such as: C #

Using system;

//Interface definition:

Interface ipoint

{

// Property Signatures:

Int x

{

Get;

SET;

}

Int Y

{

Get;

SET;

}

}

// Interface implementation:

Class MyPoint: iPoint

{

// Fields:

Private int myx;

Private int myy;

// Constructor:

Public mypoint (int X, int y)

{

MYX = X;

MYY = Y;

}

// Property Implementation:

Public Int X

{

get

{

Return myx;

}

set

{

MYX = VALUE;

}

}

Public int y

{

get

{

Return myy;

}

set

{

MYY = Value;

}

}

}

// Master:

Class mainclass

{

Private Static Void PrintPoint (iPoint P)

{

Console.writeline ("x = {0}, y = {1}", p.x, p.y);

}

Public static void

Main

()

{

MyPoint P = New MyPoint (2, 3);

Console.write ("My Point:");

PRINTPOINT (P);

}

}

Output

My Point: x = 2, y = 3 (This case is taken from MSDN, because it is very comprehensive to write)

As can be seen from the above, the interface provides a set of method definitions (of course, the above is attribute.) This is the definition of the interface in .NET.

Due to the consistency of language. So this interface is also defined as follows;

__interface This keyword is to illustrate the hosted C to implement the interface instead of a class.

__interface ImyInterface

{

HRESULT commitX ();

HRESULT GET_X (BSTR * PBSTRNAME);

}

This looks better than Virtual HRESULT Commitx () = 0; this looks far suitable for .NET language.

Below we are here to discuss its suitable condition.

1. It can be inherited from a live plurality of excuses, and of course, it is not to inherit is like me above.

2. But you can't derive it from the class

3. Of course, if you want to use C style as:

__gc __interface iMyinterface

{

PUBLIC:

Virtual hResult commitx () = 0;

Virtual HRESULT GET_X (BSTR * PBSTRNAME) = 0;

}

You may have noticed that __gc, yes, this means that you want to run in the host. This looks like the interface in COM is very similar.

4. Because it is an interface, there is no data definition. So, of course, there is no constructor and decchor, natural overload operator.

There is no reason to exist.

5. Tell me what static data is ???? In many cases, static data is used as a global data, in fact,

It is given in the initial stage of the program. Of course, if this is not established in the interface, it is not possible in the interface.

Define static data.

6. In fact, I have said that this is already concealed, it is not defined in the interface.

Ok, finally still give an example:

#define _atl_attributes 1

#include

#include

#include

#include

#include

[Module (Name = "Test")];

[Object, UUID ("00000000-0000-0000-0000-0000000001"), library_block]

__interface ifce {

[ID (0)] int int_data;

[ID (5)] BSTR BSTR_DATA;

}

[Coclass, UUID ("00000000-0000-0000-0000-0000000002")]

Class myclass: public ifce {

Private:

INT M_I;

BSTR M_BSTR;

PUBLIC:

Myclass () {

m_i = 0;

m_bstr = 0;

}

~ Myclass () {

IF (M_BSTR)

:: sysfreestring (m_bstr);

}

INT get_INT_DATA () {

Return M_i;

}

Void Put_INT_DATA (INT_I) {

m_i = _i;

}

BSTR GET_BSTR_DATA () {

BSTR BSTR = :: sysallocstring (m_bstr);

Return BSTR;

}

Void Put_BSTR_DATA (BSTR BSTR) {

IF (M_BSTR)

:: sysfreestring (m_bstr);

m_bstr = :: SysallocString (BSTR);

}

}

Int main () {

_BSTR_T BSTR ("Testing");

Coinitialize (NULL);

CCOMOBJECT * p;

CCOMOBJECT :: CreateInstance (& P);

P-> INT_DATA = 100;

Printf ("P-> INT_DATA =% D / N", P-> INT_DATA);

P-> BSTR_DATA = BSTR;

Printf ("BSTR_DATA =% S / N", P-> BSTR_DATA);

}

OUTPUT

P-> int_data = 100

BSTR_DATA = Testing

Seeing the example above, you will say me, is there a panic? No, I am not in the example.

Define data in the interface, but define properties

Reference status;

Http://www.c-sharpcorner.com/articles/migratingexistingappstodotNet1jg.asp

转载请注明原文地址:https://www.9cbs.com/read-27443.html

New Post(0)