设计模式中的"接口"的在C++用什么实现?

xlander 2007-08-11 01:20:46
设计模式中的"接口"的在C++用什么实现?

是不是用一个包含纯虚函数的类?
...全文
495 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xlander 2007-08-20
  • 打赏
  • 举报
回复
讨论得很彻底,解决我心中的很多疑问。谢谢大家了。结贴。
BenjaminHuang 2007-08-19
  • 打赏
  • 举报
回复
该说的大家都说过了,我也没有什么要补充的了,咔咔。

只是觉得实现接口模式的方式很多,COM是跨平台跨语言的最好的参考实例,C++的纯虚函数是Java 中 Interface 的来源。
zhaijunlong 2007-08-19
  • 打赏
  • 举报
回复
上面所说的interface是微软为了Com加入的关键字,和C++标准无关。C++中没有用关键字强制规定一个所谓的接口,如果想要效仿Java的interface,还是要用关键字class,自己来控制不要加入函数的实现和任何变量。如:
class SomeInterface
{
virtual int Function1()=0;
virtual int Function2()=0;
};
星羽 2007-08-17
  • 打赏
  • 举报
回复
interface



objbase.h

中定义
星羽 2007-08-17
  • 打赏
  • 举报
回复


#define interface struct
星羽 2007-08-17
  • 打赏
  • 举报
回复
interface IFile
{
virtual int read() = 0;
virtual int write() = 0;

...
}
zsdhust 2007-08-17
  • 打赏
  • 举报
回复
好像也有interface吧?
Johnny_sheng 2007-08-14
  • 打赏
  • 举报
回复
一般使用虚基类,最好是纯虚类,不允许其实例化。
LeoDiao 2007-08-14
  • 打赏
  • 举报
回复
以前写的,没有整理,先将代码贴出希望有用:
动态链接库代码:
/* FastString.cpp */

#include <cstring>
#include <iostream>
#include "FastString.h"
#include "IFastString.h"
//__declspec(dllexport)
IFastString *CreateFastString(const char *psz)
{
return new FastString(psz);
}
FastString::FastString(const char *psz)
: m_cch(strlen(psz)), m_psz(new char[m_cch+1])
{
strcpy(m_psz, psz);
}

FastString::~FastString(void)
{
std::cout<<"~FastString(void)"<<std::endl;
delete [] m_psz;
}

void FastString::Delete(void)
{
delete this;
}
int FastString::Length(void) const
{
return m_cch;
}
int FastString::Find(const char *psz) const
{
//no use now.
return 0;
}

/* FastString.h */

#include "IFastString.h"

#pragma once

class FastString :
public IFastString
{
const int m_cch;
char *m_psz;
public:
FastString(const char *psz);
~FastString(void);
void Delete(void);
int Length(void) const;
int Find(const char *psz) const;
};

/* IFastString.h */
#pragma once

#include "Test_Export.h"

class IFastString
{
public:
virtual void Delete(void) = 0;
virtual int Length(void) const = 0;
virtual int Find(const char *psz) const = 0;
};
TEST_FASTSTRING_Export
IFastString *CreateFastString(const char *psz);

/* Test_Export.h */

#if defined (TEST_FASTSTRING_AS_STATIC_LIBS)

# if !defined (TEST_FASTSTRING_HAS_DLL)
# define TEST_FASTSTRING_HAS_DLL 0
# endif /* ! TEST_FASTSTRING_HAS_DLL */
#else
# if !defined (TEST_FASTSTRING_HAS_DLL)
# define TEST_FASTSTRING_HAS_DLL 1
# endif /* ! TEST_FASTSTRING_HAS_DLL */
#endif /* TEST_FASTSTRING_AS_STATIC_LIB */

#if defined (TEST_FASTSTRING_HAS_DLL)
# if (TEST_FASTSTRING_HAS_DLL == 1)
# if defined (TEST_FASTSTRING_BUILD_DLL)
# define TEST_FASTSTRING_Export __declspec(dllexport)
# else
# define TEST_FASTSTRING_Export __declspec(dllimport)
# endif /* TEST_FASTSTRING_BUILD_DLL */
# else
# define TEST_FASTSTRING_Export
# endif /* ! TEST_FASTSTRING_HAS_DLL == 1 */
#else
# define TEST_FASTSTRING_Export
#endif /* TEST_FASTSTRING_HAS_DLL */

然后是给调用程序(及客户程序)提供的头文件

/* Test.h */

#pragma once
//#define TEST_FASTSTRING_AS_STATIC_LIBS
#define TEST_FASTSTRING_BUILD_DLL 1
#define TEST_FASTSTRING_BUILD_DLL

客户程序:
/* TestMain.cpp */
#if !defined (TEST_PRAGMA_COMMENT_FASTSTRING_LIB)
#define TEST_PRAGMA_COMMENT_FASTSTRING_LIB
#pragma comment (lib, "../Release/FastString.lib")
#endif /* TEST_PRAGMA_COMMENT_FASTSTRING_LIB */

#include <iostream>
using namespace std;

#include "../FastString/Test.h" //路径一定要正确
#include "../FastString/IFastString.h" //路径一定要正确
int main(int argc, char *argv[])
{
IFastString * pfs = CreateFastString("Hello, world!");
if(pfs)
{
pfs->Find("ello");
pfs->Delete();
}

return 0;
}
taodm 2007-08-13
  • 打赏
  • 举报
回复
类就可以了,纯虚更好,再加protected的构造/析构函数就更更好。
believefym 2007-08-12
  • 打赏
  • 举报
回复
设计模式中的接口我看只要是能被继承的类都行吧,不一定要纯需类

java中的接口才有纯需类的味道
chon81 2007-08-12
  • 打赏
  • 举报
回复
纯虚类, 当然在C++中不一定要全是纯虚函数.
laiwusheng 2007-08-11
  • 打赏
  • 举报
回复
虚基类

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧