如何用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 ", color);  
  11.     ReleaseInstance(&pApple);  
  12.     assert(pApple == 0);  
  13.     return 0;  
  14. }  


可以用 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.   
    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  
原文地址:https://www.cnblogs.com/Vae1990Silence/p/4335119.html