转载:原文:http://blog.csdn.net/caspiansea/article/details/9676153

本文给出了一种方法。基本思想是,写一个 wrapper文件,把 C++类封装起来,对外只提供C语言的接口,和 C++i相关的都在  wrapper的实现文件里实现。

1. apple.h

[cpp] view plaincopy print?
  1. #ifndef __APPLE_H__
  2. #define __APPLE_H__
  3. class Apple
  4. {
  5. public:
  6. enum
  7. {
  8. APPLE_COLOR_RED,
  9. APPLE_COLOR_BLUE,
  10. APPLE_COLOR_GREEN,
  11. };
  12. Apple();
  13. int GetColor(void);
  14. void SetColor(int color);
  15. private:
  16. int m_nColor;
  17. };
  18. #endif

apple.cpp:

[cpp] view plaincopy print?
  1. #include "apple.h"
  2. Apple::Apple():m_nColor(APPLE_COLOR_RED)
  3. {
  4. }
  5. void Apple::SetColor(int color)
  6. {
  7. m_nColor = color;
  8. }
  9. int Apple::GetColor(void)
  10. {
  11. return m_nColor;
  12. }

2. AppleWrapper.h

[cpp] view plaincopy print?
  1. #ifndef _APPLE_WRAPPER_H__
  2. #define _APPLE_WRAPPER_H_
  3. struct tagApple;
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. struct tagApple *GetInstance(void);
  8. void ReleaseInstance(struct tagApple **ppInstance);
  9. extern void SetColor(struct tagApple *pApple, int color);
  10. extern int GetColor(struct tagApple *pApple);
  11. #ifdef __cplusplus
  12. };
  13. #endif
  14. #endif

AppleWrapper.cpp

[cpp] view plaincopy print?
  1. #include "AppleWrapper.h"
  2. #include "apple.h"
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. struct tagApple
  7. {
  8. Apple apple;
  9. };
  10. struct tagApple *GetInstance(void)
  11. {
  12. return new struct tagApple;
  13. }
  14. void ReleaseInstance(struct tagApple **ppInstance)
  15. {
  16. delete *ppInstance;
  17. *ppInstance = 0;
  18. }
  19. void SetColor(struct tagApple *pApple, int color)
  20. {
  21. pApple->apple.SetColor(color);
  22. }
  23. int GetColor(struct tagApple *pApple)
  24. {
  25. return pApple->apple.GetColor();
  26. }
  27. #ifdef __cplusplus
  28. };
  29. #endif

3. test.c

[cpp] view plaincopy print?
  1. #include "AppleWrapper.h"
  2. #include <assert.h>
  3. int main(void)
  4. {
  5. struct tagApple * pApple;
  6. pApple= GetInstance();
  7. SetColor(pApple, 1);
  8. int color = GetColor(pApple);
  9. printf("color = %d\n", color);
  10. ReleaseInstance(&pApple);
  11. assert(pApple == 0);
  12. return 0;
  13. }

可以用 GCC编译:

[plain] view plaincopy print?
  1. g++ -c apple.cpp
  2. g++ -c apple.cpp  AppleWrapper.cpp
  3. gcc test.c -o test AppleWrapper.o apple.o -lstdc++

其实,  wrapper里的 struct 完全可以不要,定义一个  handle更好:

[html] view plaincopy print?
  1. #ifndef _APPLE_WRAPPER_H__
  2. #define _APPLE_WRAPPER_H_
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. int  GetInstance(int *handle);
  7. void ReleaseInstance(int *handle);
  8. extern void SetColor(int handle, int color);
  9. extern int GetColor(int handle);
  10. #ifdef __cplusplus
  11. };
  12. #endif
  13. #endif
[html] view plaincopy print?
  1. #include "AppleWrapper.h"
  2. #include "apple.h"
  3. #include <vector>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. static std::vector<Apple *> g_appleVector;
  8. int GetInstance(int * handle)
  9. {
  10. g_appleVector[0] =  new Apple;
  11. *handle = 0;
  12. return 1;
  13. }
  14. void ReleaseInstance(int *handle)
  15. {
  16. delete g_appleVector[*handle];
  17. *handle = -1;
  18. }
  19. void SetColor(int handle, int color)
  20. {
  21. g_appleVector[handle]->SetColor(color);
  22. }
  23. int GetColor(int handle)
  24. {
  25. return g_appleVector[handle]->GetColor();
  26. }
  27. #ifdef __cplusplus
  28. };
  29. #endif

如何用C语言封装 C++的类相关推荐

  1. 如何用C#语言构造蜘蛛程序

    "蜘蛛"(Spider)是Internet上一种很有用的程序,搜索引擎利用蜘蛛程序将Web页面收集到数据库,企业利用蜘蛛程序监视竞争对手的网站并跟踪变动,个人用户用蜘蛛程序下载We ...

  2. 转:如何用C#语言构造蜘蛛程序

    如何用C#语言构造蜘蛛程序 "蜘蛛"(Spider)是Internet上一种很有用的程序,搜索引擎利用蜘蛛程序将Web页面收集到数据库,企业利用蜘蛛程序监视竞争对手的网站并跟踪变动 ...

  3. [转载]如何用C#语言构造蜘蛛程序

    [转载]如何用C#语言构造蜘蛛程序 出处:未知了.. "蜘蛛"(Spider)是Internet上一种很有用的程序,搜索引擎利用蜘蛛程序将Web页面收集到数据库,企业利用蜘蛛程序监 ...

  4. Hadoop的Python语言封装

    Hadoop的Python语言封装 Gao Ang 发表于 2010年05月25日 11:38 | Hits: 245 Hadoop使 用Java语言实现,编写具体的应用业务除了借助Hadoop的Ja ...

  5. Java语言程序设计D实验——类与对象实验

    Java语言程序设计D实验--类与对象实验 一.实验内容描述(问题域描述) [实验题目]类与对象 [实验目的]使用类来封装对象的属性和功能:掌握类变量与实例变量,以及类方法与实例方法的区别:掌握使用p ...

  6. 自己封装的CMusic类 【转】

    http://www.cnblogs.com/zhangminaxiang/archive/2013/02/27/2936011.html 缘由: 在改正俄罗斯方块程序的功能的时候,想给这个程序增加一 ...

  7. R-GIS: 如何用R语言实现GIS地理空间分析及模型预测

    前言:随着地理信息系统(GIS)和大尺度研究的发展,空间数据的管理.统计与制图变得越来越重要.R语言在数据分析.挖掘和可视化中发挥着重要的作用,其中在空间分析方面扮演着重要角色,与空间相关的包的数量也 ...

  8. 511遇见易语言封装免注册免查杀大漠模块命令

    高清视频,演示了如何免注册封装大漠模块,封装了大漠文档的几乎所有命令,并针对相应的命令选择的做了测试,示范,调用. 目录: 1-单线程免注册免查杀 2-单线程后台绑定窗口BindWindow 3-后台 ...

  9. 易语言封装免注册免查杀大漠模块命令

    大漠图色插件时COM组件,我们可以封装成模块,可以免注册到系统,直接调用接口,有效避免杀软的拦截,查杀,也可以再dm.dll释放时,改变文件的属性,比如系统.存档.只读.隐藏,你也可以给它改名成360 ...

  10. C语言面向对象编程的类是指,c语言面向对象编程中的类_C ++中的面向对象编程...

    c语言面向对象编程中的类 Object Oriented programming is a programming style that is associated with the concept ...

最新文章

  1. 德国SNS交友/视频网站Poppen.de的技术架构分享
  2. 第三十四课.模糊神经网络
  3. Python(8):模块内置变量
  4. 不要再把 pp 写出 % 了。
  5. pom.xml中依赖的optionaltrue/optional标签
  6. 华为鸿蒙联合品牌,魅族官宣:接入华为鸿蒙!这是国产智能手机品牌的首个公开表态!...
  7. 偶数支足球队进行单循环比赛,按照指定算法打印每轮的对阵形势
  8. 新版谷歌开启flash的方法
  9. 优质计算机教案,信息技术优秀教案
  10. 树莓派 网络附加存储NAS系统和USB外接硬盘文件服务器
  11. 做个合格的吃货~利用Python爬取美食网站3032个菜谱并分析
  12. java zero fill_一次JavaAssist引发的生产环境CPU报警的问题
  13. google-auto之自动生成组件化文件
  14. EF6 T4 Model.TT文件的修改-自动加上注释
  15. 简化字与繁体字的关系
  16. php订阅号发送消息,php实现微信公众号主动推送消息
  17. cosx绝对值的积分
  18. 2014 CVPR-DeepReID Deep Filter Pairing Neural Network for Person Re-Identification
  19. Idea将Java文件导出jar包
  20. 点云数据详解——点云数据变为图像

热门文章

  1. Joplin 的思维导图 Mindmap(脑图)插件
  2. iPhone/iPad解锁屏幕密码
  3. MTK平台TP驱动详解
  4. 科普一下:1G, 2G, 3G,4G,5G历史发展和定义
  5. 对《人工智能的进化》这一人工智能方面科普书籍的学习/摘抄/总结
  6. 自主移动机器人模型制作
  7. 任天堂Switch最优DNS测试
  8. 如何离线加载全国2.48TB天地图
  9. 谷歌浏览器部分iframe页面无法打开,跨域问题
  10. 【进制转换】如何使用C++将2进制转换为16进制?