LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1256|回复: 10

如何用多态的方法,只将接口的头文件暴露,隐藏其他的一切。

[复制链接]
发表于 2006-7-25 19:34:25 | 显示全部楼层 |阅读模式
只象暴露Interface.h接口和派生类.o文件给用户,派生类的头文件也隐藏。主要的问题如何在客户端将派生类InterfaceImpl的指针赋值给接口Interface.
Interface.h//

  1. #define interface struct
  2. interface DemoInterface
  3. {
  4. public:
  5.          virtual void SetX(int length) = 0;
  6.          virtual long GetX() const = 0;
  7. }
复制代码

InterfaceImpl.h//

  1. class DemoInterfaceImpl : public DemoInterface
  2. {
  3. public:
  4.          virtual void SetX(int length) ;
  5.          virtual long GetX() const ;
  6. }
复制代码

InterfaceImpl.cpp//

  1. void DemoInterfaceImpl::SetX(int length)
  2. {
  3. }
  4. int DemoInterfaceImpl::GetX() const
  5. {
  6. }
复制代码
发表于 2006-7-25 23:05:46 | 显示全部楼层
看《设计模式》中的brige
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-7-26 10:17:05 | 显示全部楼层
自己琢磨一下是这样写的:
Interface.h//

  1. #define interface struct

  2. interface IXMLReaderContext
  3. {
  4. public:
  5.       virtual void AddAttribute() const = 0;
  6.       virtual void Display() const = 0;
  7. };

  8. extern IXMLReaderContext* Initialize();
复制代码

InterfaceImpl.h//

  1. #include "Interface.h"

  2. class TVReader : public IXMLReaderContext
  3. {
  4. public:
  5.       TVReader(){}
  6.       ~TVReader(){}

  7.       void AddAttribute() const;
  8.       void Display() const;
  9. };
复制代码

InterfaceImpl.cpp//

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "InterfaceImpl.h"

  4. void TVReader::AddAttribute() const
  5. {
  6. }

  7. void TVReader::Display() const
  8. {
  9.    printf("Inside TVReader...\n");
  10. }
复制代码

Others.cpp

  1. #include "InterfaceImpl.h"

  2. IXMLReaderContext*  Initialize()
  3. {
  4.       TVReader* pTVReader = new TVReader;
  5.       return pTVReader;
  6. }
复制代码

调试结果:

  1. [root@root vitual]# g++ -c InterfaceImpl.cpp
  2. [root@root vitual]# g++ -c Others.cpp
  3. [root@root vitual]# g++ -c Client.cpp
  4. [root@root vitual]# ls
  5. Client.o     InterfaceImpl.cpp  InterfaceImpl.o  Others.cpp
  6. Client.cpp  Interface.h  InterfaceImpl.h    LIBS            Others.o

  7. [root@root vitual]# mv InterfaceImpl.h InterfaceImpl.cpp   Others.cpp LIBS/
  8. [root@root vitual]# ls
  9. Client.cpp  Client.o  Interface.h  InterfaceImpl.o  libs  Others.o
  10. [root@root vitual]# g++ -o a.out Client.o InterfaceImpl.o  Others.o
  11. [root@root vitual]# ls
  12. a.out  Client.cpp  Client.o  Interface.h  InterfaceImpl.o  libs  Others.o
  13. [root@root vitual]# ./a.out
  14. Inside TVReader...
  15. [root@root vitual]#
复制代码

这种投机取巧的方法为那些将开源代码商业化提供了一种思路。提供玩玩的一种方法。
回复 支持 反对

使用道具 举报

发表于 2006-8-14 22:07:35 | 显示全部楼层
Post by sybaselu

这种投机取巧的方法为那些将开源代码商业化提供了一种思路。提供玩玩的一种方法。


并非什么投机取巧,这种方法很常见。
在Effective C++ 条款34中有更具体的说明。
回复 支持 反对

使用道具 举报

发表于 2006-8-15 10:01:57 | 显示全部楼层
Lolita 小姐译的 http://www.linuxsir.cn/bbs/showthread.php?t=266890 就使用了类似的trick
回复 支持 反对

使用道具 举报

发表于 2006-8-15 10:27:32 | 显示全部楼层
Post by rickxbx
Lolita 小姐译的 http://www.linuxsir.cn/bbs/showthread.php?t=266890 就使用了类似的trick

rickxbx大哥不厚道啊
头像是Envelop(接口),性别是Letter(细节)
用美女作头像的GG比比皆是:confused:
回复 支持 反对

使用道具 举报

发表于 2006-8-15 10:35:45 | 显示全部楼层
Post by Lolita
rickxbx大哥不厚道啊
头像是Envelop(接口),性别是Letter(细节)
用美女作头像的GG比比皆是:confused:


洛丽塔可是知名美女哈 "一树梨花压海棠"的女主角不就叫洛丽塔嘛,不要告诉我,你的洛丽塔是瞎起的
回复 支持 反对

使用道具 举报

发表于 2006-8-15 10:49:17 | 显示全部楼层
Post by rickxbx
洛丽塔可是知名美女哈 "一树梨花压海棠"的女主角不就叫洛丽塔嘛,不要告诉我,你的洛丽塔是瞎起的
  -删去与主题无关的字N个-
回复 支持 反对

使用道具 举报

发表于 2006-8-15 11:40:52 | 显示全部楼层
楼主在看FIREFOX的代码吗????
回复 支持 反对

使用道具 举报

发表于 2006-8-15 17:10:04 | 显示全部楼层
从楼主最近的发帖来看, 像是在写数据库方面的应用
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表