【转载】如何用C语言封装 C++的类,在 C里面使用

本文给出了一种方法。基本思想是,写一个 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编译:

g++ -c apple.cpp
g++ -c apple.cpp  AppleWrapper.cpp
gcc test.c -o test AppleWrapper.o apple.o -lstdc++
注意:lstdc++ 该选项不添加时,当有调用到标准库函数时,会提示undefined错误


其实,  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



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值