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?
- #ifndef __apple_h__
- #define __apple_h__
- Class Apple
- {
- Public
- enum
- {
- Apple_color_red,
- Apple_color_blue,
- Apple_color_green,
- };
- Apple ();
- int GetColor (void);
- void setcolor (int color);
- Private
- int m_ncolor;
- };
- #endif
Apple.cpp:
[CPP]View Plaincopyprint?
- #include "apple.h"
- Apple::apple (): M_ncolor (apple_color_red)
- {
- }
- void Apple::setcolor (int color)
- {
- M_ncolor = color;
- }
- int Apple::getcolor (void)
- {
- return m_ncolor;
- }
2. AppleWrapper.h
[CPP]View Plaincopyprint?
- #ifndef _apple_wrapper_h__
- #define _apple_wrapper_h_
- struct tagapple;
- #ifdef __cplusplus
- extern "C" {
- #endif
- struct Tagapple *getinstance (void);
- void ReleaseInstance (struct tagapple **ppinstance);
- extern void SetColor (struct tagapple *papple, int color);
- extern int GetColor (struct tagapple *papple);
- #ifdef __cplusplus
- };
- #endif
- #endif
AppleWrapper.cpp
[CPP]View Plaincopyprint?
- #include "AppleWrapper.h"
- #include "apple.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- struct Tagapple
- {
- Apple Apple;
- };
- struct Tagapple *getinstance (void)
- {
- return new struct tagapple;
- }
- void ReleaseInstance (struct tagapple **ppinstance)
- {
- Delete *ppinstance;
- *ppinstance = 0;
- }
- void SetColor (struct tagapple *papple, int color)
- {
- Papple->apple. SetColor (color);
- }
- int GetColor (struct tagapple *papple)
- {
- return papple->apple. GetColor ();
- }
- #ifdef __cplusplus
- };
- #endif
3. test.c
[CPP]View Plaincopyprint?
- #include "AppleWrapper.h"
- #include <assert.h>
- int main (void)
- {
- struct tagapple * papple;
- Papple= getinstance ();
- SetColor (Papple, 1);
- int color = GetColor (papple);
- printf ("color =%d\n", color);
- ReleaseInstance (&papple);
- ASSERT (Papple = = 0);
- return 0;
- }
Can be compiled with GCC:
[Plain]View Plaincopyprint?
- g++-C Apple.cpp
- g++-C Apple.cpp AppleWrapper.cpp
- 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?
- #ifndef _apple_wrapper_h__
- #define _apple_wrapper_h_
- #ifdef __cplusplus
- extern "C" {
- #endif
- int getinstance (int *handle);
- void releaseinstance (int *handle);
- extern void SetColor (int handle, int color);
- extern int GetColor (int handle);
- #ifdef __cplusplus
- };
- #endif
- #endif
[HTML]View Plaincopyprint?
- #include "AppleWrapper.h"
- #include "apple.h"
- #include <vectors>
- #ifdef __cplusplus
- extern "C" {
- #endif
- Static Std::vector<Apple *> G_applevector;
- int getinstance (int * handle)
- {
- G_applevector[0] = new Apple;
- *handle = 0;
- return 1;
- }
- void releaseinstance (int *handle)
- {
- Delete G_applevector[*handle];
- *handle =-1;
- }
- void SetColor (int handle, int color)
- {
- g_applevector[handle]->setcolor (color);
- }
- int GetColor (int handle)
- {
- return g_applevector[handle]->getcolor ();
- }
- #ifdef __cplusplus
- };
- #endif
How to encapsulate a C + + class in the C. language using C