博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】如何用C语言封装 C++的类,在 C里面使用
阅读量:4110 次
发布时间:2019-05-25

本文共 3123 字,大约阅读时间需要 10 分钟。

本文给出了一种方法。基本思想是,写一个 wrapper文件,把 C++类封装起来,对外只提供C语言的接口,和 C++i相关的都在  wrapper的实现文件里实现。
1. apple.h
#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:
#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
#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
#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
#include "AppleWrapper.h" 
#include
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编译:
g++ -c apple.cppg++ -c apple.cpp  AppleWrapper.cppgcc test.c -o test AppleWrapper.o apple.o -lstdc++
注意:lstdc++ 该选项不添加时,当有调用到标准库函数时,会提示undefined错误
其实,  wrapper里的 struct 完全可以不要,定义一个  handle更好:
#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
#include "AppleWrapper.h" 
#include "apple.h"
#include
#ifdef __cplusplus
extern "C" {
#endif
static std::vector
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://pdosi.baihongyu.com/

你可能感兴趣的文章
application/x-www-form-urlencoded、multipart/form-data、text/plain
查看>>
关于Content-Length
查看>>
WebRequest post读取源码
查看>>
使用TcpClient可避免HttpWebRequest的常见错误
查看>>
EntityFramework 学习之一 —— 模型概述与环境搭建 .
查看>>
C# 发HTTP请求
查看>>
初试visual studio2012的新型数据库LocalDB
查看>>
启动 LocalDB 和连接到 LocalDB
查看>>
Palindrome Number --回文整数
查看>>
Reverse Integer--反转整数
查看>>
Container With Most Water --装最多水的容器(重)
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Generate Parentheses--生成匹配括号(重)
查看>>
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>
Count and Say
查看>>
Gas Station
查看>>