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

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

1. apple.h

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

  1. #include "apple.h"  
  2. Apple::Apple():m_nColor(APPLE_COLOR_RED)  
  3. {  
  4. }  
  5.   
  6. void Apple::SetColor(int color)  
  7. {  
  8.     m_nColor = color;  
  9. }  
  10.   
  11. int Apple::GetColor(void)  
  12. {  
  13.     return m_nColor;  
  14. }  

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.   
  4. #ifdef __cplusplus  
  5. extern "C" {  
  6. #endif  
  7. struct tagApple  
  8. {  
  9.     Apple apple;  
  10. };  
  11. struct tagApple *GetInstance(void)  
  12. {  
  13.     return new struct tagApple;  
  14. }  
  15. void ReleaseInstance(struct tagApple **ppInstance)  
  16. {  
  17.     delete *ppInstance;  
  18.     *ppInstance = 0;  
  19.       
  20. }  
  21. void SetColor(struct tagApple *pApple, int color)  
  22. {  
  23.     pApple->apple.SetColor(color);  
  24. }  
  25.   
  26. int GetColor(struct tagApple *pApple)  
  27. {  
  28.     return pApple->apple.GetColor();  
  29. }  
  30. #ifdef __cplusplus  
  31. };  
  32. #endif  

3. test.c

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

可以用 GCC编译:

[plain]  view plain copy 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 plain copy 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 plain copy print ?
  1. #include "AppleWrapper.h"  
  2. #include "apple.h"  
  3. #include <vector>  
  4.   
  5. #ifdef __cplusplus  
  6. extern "C" {  
  7. #endif  
  8.   
  9. static std::vector<Apple *> g_appleVector;  
  10.   
  11. int GetInstance(int * handle)  
  12. {  
  13.     g_appleVector[0] =  new Apple;  
  14.     *handle = 0;  
  15.     return 1;  
  16. }  
  17. void ReleaseInstance(int *handle)  
  18. {  
  19.     delete g_appleVector[*handle];  
  20.     *handle = -1;  
  21.       
  22. }  
  23. void SetColor(int handle, int color)  
  24. {  
  25.     g_appleVector[handle]->SetColor(color);  
  26. }  
  27.   
  28. int GetColor(int handle)  
  29. {  
  30.     return g_appleVector[handle]->GetColor();  
  31. }  
  32. #ifdef __cplusplus  
  33. };  
  34. #endif  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值