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

1. apple.h

  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:

  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

  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

  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

  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编译:

  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更好:

  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
    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++的类,在 C里面使用的更多相关文章

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

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

  2. 使用libzplay库封装一个音频类

    装载请说明原地址,谢谢~~      前两天我已经封装好一个duilib中使用的webkit内核的浏览器控件和一个基于vlc的用于播放视频的视频控件,这两个控件可以分别用在放酷狗播放器的乐库功能和MV ...

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

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

  4. .NET3.5中JSON用法以及封装JsonUtils工具类

    .NET3.5中JSON用法以及封装JsonUtils工具类  我们讲到JSON的简单使用,现在我们来研究如何进行封装微软提供的JSON基类,达到更加方便.简单.强大且重用性高的效果. 首先创建一个类 ...

  5. MySQL数据库学习笔记(十一)----DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  6. MySQL数据库学习笔记(十)----JDBC事务处理、封装JDBC工具类

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. JAVA中封装JSONUtils工具类及使用

    在JAVA中用json-lib-2.3-jdk15.jar包中提供了JSONObject和JSONArray基类,用于JSON的序列化和反序列化的操作.但是我们更习惯将其进一步封装,达到更好的重用. ...

  8. MySQL JDBC事务处理、封装JDBC工具类

    MySQL数据库学习笔记(十)----JDBC事务处理.封装JDBC工具类 一.JDBC事务处理: 我们已经知道,事务的概念即:所有的操作要么同时成功,要么同时失败.在MySQL中提供了Commit. ...

  9. DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

    DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类) 一.DAO模式简介 DAO即Data Access Object,数据访问接口.数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据 ...

随机推荐

  1. HDU 3339 最短路+01背包

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. HDU 5171 GTY's birthday gift 矩阵快速幂

    GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  3. PAT (Advanced Level) 1014. Waiting in Line (30)

    简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...

  4. 【python】一个备份把文件备份到邮箱的python实现

    公司服务器弄了跳板机,从服务器上拉文件变得好麻烦,弄了个脚本从服务器上向邮箱发送文件,还蛮方便哈- #!/usr/bin/env python2.7 #! coding:UTF-8 import sm ...

  5. java SWT嵌入IE,SafeArray .

    java SWT嵌入IE,SafeArray );    // Create a by ref variant    Variant variantByRef = new Variant(pVaria ...

  6. Android 4.0 ICS SystemUI浅析——StatusBar结构分析

    Android 4.0 ICS SystemUI浅析——StatusBar结构分析 分类: Android2012-06-30 14:45 23687人阅读 评论(8) 收藏 举报 androidsi ...

  7. (简单) POJ 2251 Dungeon Master,BFS。

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  8. Jquery使用常见(全)

    一.选择器实例 语法 描述 $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 class=&q ...

  9. sublime text 调出结果输出框

    sublime是一个非常好用的代码编辑器,同时可以build program 但是在执行代码的过程中,如果进行了查找等操作,下面原来显示输出框的地方被查找界面替代,而程序结果输出框就会"消失 ...

  10. Spring 工作原理

    1.spring原理 内部最核心的就是IOC了,动态注入,让一个对象的创建不用new了,可以自动的生产,这其实就是利用java里的反射,反射其实就是在运行时动态的去创建.调用对象,Spring就是在运 ...