如何用C语言封装 C++的类,在 C里面使用
2015-12-26 19:02
381 查看
本文给出了一种方法。基本思想是,写一个 wrapper文件,把 C++类封装起来,对外只提供C语言的接口,和 C++i相关的都在 wrapper的实现文件里实现。
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;
}
可以用 GCC编译:
[plain] view
plaincopyprint?
g++ -c apple.cpp
g++ -c apple.cpp AppleWrapper.cpp
gcc test.c -o test AppleWrapper.o apple.o -lstdc++
其实, wrapper里的 struct 完全可以不要,定义一个 handle更好:
[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 <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
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;
}
可以用 GCC编译:
[plain] view
plaincopyprint?
g++ -c apple.cpp
g++ -c apple.cpp AppleWrapper.cpp
gcc test.c -o test AppleWrapper.o apple.o -lstdc++
其实, wrapper里的 struct 完全可以不要,定义一个 handle更好:
[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 <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
相关文章推荐
- 封装好的Folyd建图,C++源码
- matlab与c/c++混合编程
- matlab与c/c++混合编程——c/c++调用matlab
- C/C++常用的调试宏
- VC内存泄露检查工具:VisualLeakDetector
- 内联函数总结
- usb体系结构
- VC结构体实现类似数组的下标操作符功能
- C/C++ 嵌套结构体动态内存管理实现
- C++调用gSoap编写的WEBSERVICE与C#.NET间接口自定义结构体不能重复使用
- 处理VC开发的webservice在C#.NET中中文乱码问题
- gcc使用入门
- C/C++ Linux 程序员必须了解的 10 个工具
- 递归遍历数组
- 用C/C++打印*号图案
- C++之四书五经(上)
- C++中const的使用
- *p++和*++p的区别
- C/C++输入读取数据函数总结
- 不可表示的数(庞果网)完全解答