"In VC"

xiaoxiao2021-03-06  103

The interface is a special class that is not implemented, it is a collection of operations, we can think of it as an agreement with other objects. There is no keyword such as Interface in C to define interfaces, but the __Declspec (NOVTABLE) is provided in Mircrosoft C to modify a class, which means that the class has no virtual functions, that is, the virtual function is pure. So use it we can still define an interface. The code example is as follows:

#include

<

Iostream

>

Using

Namespace

STD;

#define

Interface class __declspec (novTable)

Interface

ICODEC {

public

:

Virtual

Bool

Decode

charr

*

LPDataSRC, unsigned

int

Nsrclen,

charr

*

LPDATADST, UNSIGNED

int

*

Pndstlen;

Virtual

Bool

ENCODE

charr

*

LPDataSRC, unsigned

int

Nsrclen,

charr

*

LPDATADST, UNSIGNED

int

*

Pndstlen;

Class

CCODEC:

public

ICODEC {

public

:

Virtual

Bool

Decode

charr

*

LPDataSRC, unsigned

int

Nsrclen,

charr

*

LPDATADST, UNSIGNED

int

*

Pndstlen) {cout

<<

"

decoding...

"

<<

ENDL;

Return

True

}

Virtual

Bool

ENCODE

charr

*

LPDataSRC, unsigned

int

Nsrclen,

charr

*

LPDATADST, UNSIGNED

int

*

Pndstlen) {cout

<<

"

coding...

"

<<

ENDL;

Return

True

;}};

int

Main

int

Argc,

charr

*

Argv []) {icodec

*

PCODEC

=

New

Ccodec (); pcodec

->

Decode (NULL,

0

, Null, null; PCODEC

->

ENCODE (NULL,

0

, Null, null; delete (CCODEC

*

PCODEC;

Return

0

}

The above ICODEC interface is equivalent to the following definition:

Class

ICODEC {

public

:

Virtual

Bool

Decode

charr

*

LPDataSRC, unsigned

int

Nsrclen,

charr

*

LPDATADST, UNSIGNED

int

*

Pndstlen

=

0

;

Virtual

Bool

ENCODE

charr

*

LPDataSRC, unsigned

int

Nsrclen,

charr

*

LPDATADST, UNSIGNED

int

*

Pndstlen

=

0

};

related articles:

What is the __Declspec in the VC - preface

Chapter 1 of the "__Declspec" in the VC (1) - Definition Interface

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

New Post(0)