其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
VC中的“__declspec”能作什么(1)-定义接口

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

??? 接口是一个没有被实现的特殊的类,它是一系列操作的集合,我们可以把它看作是与其他对象通讯的协议。C++中没有提供类似interface这样的关键字来定义接口,但是Mircrosoft c++中提供了__declspec(novtable)来修饰一个类,来表示该类没有虚函数表,也就是虚函数都是纯虚的。所以利用它我们依然可以定义一个接口。代码例子如下:

#include

using namespace std;

#define interface class __declspec(novtable)

interface ICodec
{
public:
?virtual bool Decode(char * lpDataSrc,unsigned int nSrcLen,char * lpDataDst,unsigned int *pnDstLen);
?virtual bool Encode(char * lpDataSrc,unsigned int nSrcLen,char * lpDataDst,unsigned int *pnDstLen);
};

class CCodec : public ICodec
{
public:
?virtual bool Decode(char * lpDataSrc,unsigned int nSrcLen,char * lpDataDst,unsigned int *pnDstLen)
?{
??cout << "解码..." << endl;
??return true;
?}
?virtual bool Encode(char * lpDataSrc,unsigned int nSrcLen,char * lpDataDst,unsigned int *pnDstLen)
?{
??cout << "编码..." << endl;
??return true;
?}
};

int main(int argc, char* argv[])
{
?ICodec * pCodec = new CCodec();
?pCodec->Decode(NULL,0,NULL,NULL);
?pCodec->Encode(NULL,0,NULL,NULL);
?delete (CCodec*)pCodec;
?return 0;
}
上面的ICodec接口等价于下面的定义:
class ICodec
{
public:
?virtual bool Decode(char * lpDataSrc,unsigned int nSrcLen,char * lpDataDst,unsigned int *pnDstLen)=0;
?virtual bool Encode(char * lpDataSrc,unsigned int nSrcLen,char * lpDataDst,unsigned int *pnDstLen)=0;
};
?

相关文章:

VC中的__declspec能作什么-前言

第一章 VC中的“__declspec”能作什么(1)-定义接口

第二章 VC中的“__declspec”能作什么(2)-为类增加属性????




相关文章

相关软件