当前位置:   article > 正文

如何用C语言封装 C++的类,在 C里面使用_c++ 将类封装 extern "c

c++ 将类封装 extern "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++

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


本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/43370?site
推荐阅读
相关标签
  

闽ICP备14008679号