How to encapsulate a C + + class in the C. language using C

Source: Internet
Author: User
Tags getcolor

In this paper, a method is given. The basic idea is to write a wrapper file, encapsulate the C + + class, and only provide the C language interface, and C++I related are implemented in the wrapper implementation file.

1. apple.h

[CPP]View Plaincopyprint?
  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:

[CPP]View Plaincopyprint?
    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

[CPP]View Plaincopyprint?
  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 Plaincopyprint?
  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

[CPP]View Plaincopyprint?
  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. }


Can be compiled with GCC:

[Plain]View Plaincopyprint?
    1. g++-C Apple.cpp
    2. g++-C Apple.cpp AppleWrapper.cpp
    3. GCC test.c-o test APPLEWRAPPER.O apple.o-lstdc++

In fact, wrapper in the struct completely can not, define a handle better:

[HTML]View Plaincopyprint?
    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 Plaincopyprint?
    1. #include "AppleWrapper.h"
    2. #include "apple.h"
    3. #include <vectors>
    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

How to encapsulate a C + + class in the C. language using C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.