C++接口的定义用一个实例说明
来源:互联网 发布:数据库mdf文件怎么用 编辑:程序博客网 时间:2024/04/25 21:56
接口是一个没有被实现的特殊的类,它是一系列操作的集合,我们可以把它看作是与其他对象通讯的协议。C++中没有提供类似interface这样的关键 字来定义接口,但是Mircrosoft c++中提供了__declspec(novtable)来修饰一个类,来表示该类没有虚函数表,也就是虚函数都是纯虚的。所以利用它我们依然可以定义一 个接口。代码例子如下:
#include <IOSTREAM>
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;
}
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;
};
- C++接口的定义用一个实例说明
- C++接口的定义用一个实例说明
- C++接口的定义用一个实例说明
- C++接口的定义用一个实例说明
- C++接口的定义用一个实例说明
- 实例说明接口的作用
- 接口的一个实例
- QuickCSharp框架开发(13)------定义一个IAuthenticationProvider接口、将实现了接口的实例化对象转换成接口
- Java实例 定义任务的一个发射小程序 实现Runnable接口并编写run()方法
- 一个AgileFlow的流程定义实例
- 一个AgileFlow的流程定义实例
- 一个AgileFlow的流程定义实例
- 关于Java接口interface定义的几点说明
- ibatis的sqlSessionTemplate实例的sqlSession接口定义。
- JAVA中接口的一个实例
- 回调函数教程(一):回调函数定义及用接口实现的实例
- Java泛型--泛型实例--标识接口的定义
- java中接口的作用(实例说明)
- 学好VC++的十大良好习惯
- 继承:基类和派生类
- 产生随机树,从randk 产生 randn
- 堆和栈的区别
- 编程修养(推荐阅读)
- C++接口的定义用一个实例说明
- 去
- Java类的基本运行顺序
- 重金聘请外挂高手制作港式五张外挂
- s3c6410上的lan芯片smsc9221驱动移植
- 在图片元素固定位置显示层的种方法
- 空间几何c++代码
- STL简介
- javascript窗口相关代码总结