如何用C语言封装 C++的类,在 C里面使用
2017-12-05 10:14
330 查看
wrapper的实现文件里实现。
apple.h
[cpp] view
plain copy
#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
plain copy
#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;
}
AppleWrapper.h
[cpp] view
plain copy
#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
plain copy
#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
test.c
[cpp] view
plain copy
#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;
}
可以用 GCC编译:
[cpp] view
plain copy
g++ -c apple.cpp
g++ -c apple.cpp AppleWrapper.cpp
gcc test.c -o test AppleWrapper.o apple.o -lstdc++
其实, wrapper里的 struct 完全可以不要,定义一个 handle更好:
[cpp] view
plain copy
#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
[cpp] view
plain copy
#include "AppleWrapper.h"
#include "apple.h"
#include <vector>
#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
转载地址:
http://blog.csdn.net/caspiansea/article/details/9676153
相关文章推荐
- 如何用C语言封装 C++的类,在 C里面使用
- 如何用C语言封装 C++的类,在 C里面使用
- 如何用C语言封装 C++的类,在 C里面使用 .
- 如何用C语言封装 C++的类,在 C里面使用
- 如何用C语言封装 C++的类,在 C里面使用
- 如何用C语言封装 C++的类,在 C里面使用
- 如何用C语言封装 C++的类,在 C里面使用
- 如何用C语言封装 C++的类
- 如何用C语言实现C++的基本特性:封装、继承、多态
- ubuntu使用技巧——如何编译C++和C语言的使用程序
- 如何让c语言使用结构体近似模拟c++中的类
- 如何使用别人的代码 (特指在MFC里面 或者推广为C++里面)
- 如何使用VS2013将C++派生类封装成DLL并动态调用
- (原創) 如何使用C++/CLI对图片做Grayscale Erosion? (.NET) (C/C++) (C++/CLI) (GDI+) (Image Processing)
- 如何混合使用C和C++(下)
- [超简单]C++如何使用MySQL数据库
- (原創) 如何使用C++/CLI读/写jpg檔? (.NET) (C++/CLI) (GDI+) (C/C++) (Image Processing)
- (原創) 如何使用C++/CLI对图片做Grayscale Dilation? (.NET) (C/C++) (C++/CLI) (GDI+) (Image Processing)
- C++如何使用MySQL数据库
- 如何限制在一个表格里面禁止使用右键