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

来源:互联网 发布:幼师网络研修个人计划 编辑:程序博客网 时间:2024/05/11 23:22

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

apple.h

[cpp] view plain copy
  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.     
  13.     Apple();    
  14.     int GetColor(void);    
  15.     void SetColor(int color);    
  16. private:    
  17.     int m_nColor;    
  18. };    
  19. #endif     


apple.cpp:

[cpp] view plain copy
  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. }    


 AppleWrapper.h

[cpp] view plain copy
  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 plain copy
  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  


test.c

[cpp] view plain copy
  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编译:

[cpp] view plain copy
  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更好:

[cpp] view plain copy
  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  


[cpp] view plain copy
  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  



转载地址:

http://blog.csdn.net/caspiansea/article/details/9676153


原创粉丝点击