如何用C语言封装 C++的类
转载:原文:http://blog.csdn.net/caspiansea/article/details/9676153
本文给出了一种方法。基本思想是,写一个 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 <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编译:
- g++ -c apple.cpp
- g++ -c apple.cpp AppleWrapper.cpp
- gcc test.c -o test AppleWrapper.o apple.o -lstdc++
其实, 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 <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
如何用C语言封装 C++的类相关推荐
- 如何用C#语言构造蜘蛛程序
"蜘蛛"(Spider)是Internet上一种很有用的程序,搜索引擎利用蜘蛛程序将Web页面收集到数据库,企业利用蜘蛛程序监视竞争对手的网站并跟踪变动,个人用户用蜘蛛程序下载We ...
- 转:如何用C#语言构造蜘蛛程序
如何用C#语言构造蜘蛛程序 "蜘蛛"(Spider)是Internet上一种很有用的程序,搜索引擎利用蜘蛛程序将Web页面收集到数据库,企业利用蜘蛛程序监视竞争对手的网站并跟踪变动 ...
- [转载]如何用C#语言构造蜘蛛程序
[转载]如何用C#语言构造蜘蛛程序 出处:未知了.. "蜘蛛"(Spider)是Internet上一种很有用的程序,搜索引擎利用蜘蛛程序将Web页面收集到数据库,企业利用蜘蛛程序监 ...
- Hadoop的Python语言封装
Hadoop的Python语言封装 Gao Ang 发表于 2010年05月25日 11:38 | Hits: 245 Hadoop使 用Java语言实现,编写具体的应用业务除了借助Hadoop的Ja ...
- Java语言程序设计D实验——类与对象实验
Java语言程序设计D实验--类与对象实验 一.实验内容描述(问题域描述) [实验题目]类与对象 [实验目的]使用类来封装对象的属性和功能:掌握类变量与实例变量,以及类方法与实例方法的区别:掌握使用p ...
- 自己封装的CMusic类 【转】
http://www.cnblogs.com/zhangminaxiang/archive/2013/02/27/2936011.html 缘由: 在改正俄罗斯方块程序的功能的时候,想给这个程序增加一 ...
- R-GIS: 如何用R语言实现GIS地理空间分析及模型预测
前言:随着地理信息系统(GIS)和大尺度研究的发展,空间数据的管理.统计与制图变得越来越重要.R语言在数据分析.挖掘和可视化中发挥着重要的作用,其中在空间分析方面扮演着重要角色,与空间相关的包的数量也 ...
- 511遇见易语言封装免注册免查杀大漠模块命令
高清视频,演示了如何免注册封装大漠模块,封装了大漠文档的几乎所有命令,并针对相应的命令选择的做了测试,示范,调用. 目录: 1-单线程免注册免查杀 2-单线程后台绑定窗口BindWindow 3-后台 ...
- 易语言封装免注册免查杀大漠模块命令
大漠图色插件时COM组件,我们可以封装成模块,可以免注册到系统,直接调用接口,有效避免杀软的拦截,查杀,也可以再dm.dll释放时,改变文件的属性,比如系统.存档.只读.隐藏,你也可以给它改名成360 ...
- C语言面向对象编程的类是指,c语言面向对象编程中的类_C ++中的面向对象编程...
c语言面向对象编程中的类 Object Oriented programming is a programming style that is associated with the concept ...
最新文章
- 德国SNS交友/视频网站Poppen.de的技术架构分享
- 第三十四课.模糊神经网络
- Python(8):模块内置变量
- 不要再把 pp 写出 % 了。
- pom.xml中依赖的optionaltrue/optional标签
- 华为鸿蒙联合品牌,魅族官宣:接入华为鸿蒙!这是国产智能手机品牌的首个公开表态!...
- 偶数支足球队进行单循环比赛,按照指定算法打印每轮的对阵形势
- 新版谷歌开启flash的方法
- 优质计算机教案,信息技术优秀教案
- 树莓派 网络附加存储NAS系统和USB外接硬盘文件服务器
- 做个合格的吃货~利用Python爬取美食网站3032个菜谱并分析
- java zero fill_一次JavaAssist引发的生产环境CPU报警的问题
- google-auto之自动生成组件化文件
- EF6 T4 Model.TT文件的修改-自动加上注释
- 简化字与繁体字的关系
- php订阅号发送消息,php实现微信公众号主动推送消息
- cosx绝对值的积分
- 2014 CVPR-DeepReID Deep Filter Pairing Neural Network for Person Re-Identification
- Idea将Java文件导出jar包
- 点云数据详解——点云数据变为图像