如何用C语言封装 C++的类,在 C里面使用

本文给出了一种方法。基本思想是,写一个 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


  • 23
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
好的,我会尽力回答您的问题。在C语言中,封装可以通过结构体来实现。结构体中的成员变量可以被定义为私有的,只能通过结构体提供的接口函数来访问。继承可以通过结构体嵌套来实现,子结构体可以继承父结构体中的成员变量和函数指针,并可以添加自己的成员变量和函数指针。多态可以通过函数指针来实现,父结构体中的函数指针可以指向不同的子结构体中的同名函数,从而实现多态。 下面是一个示例代码,演示了如何在C语言中实现封装、继承和多态的特性: ```c #include <stdio.h> // 父结构体 typedef struct { int x; } Base; // 子结构体 typedef struct { Base base; // 嵌套父结构体 int y; } Derived; // 父结构体的接口函数 void setX(Base *base, int x) { base->x = x; } int getX(Base *base) { return base->x; } // 子结构体的接口函数 void setY(Derived *derived, int y) { derived->y = y; } int getY(Derived *derived) { return derived->y; } // 多态的函数 void printX(Base *base) { printf("%d\n", base->x); } int main() { Derived d; setX(&d.base, 1); setY(&d, 2); printf("%d %d\n", getX(&d.base), getY(&d)); printX(&d.base); return 0; } ``` 在上面的示例代码中,我们定义了一个父结构体`Base`和一个子结构体`Derived`。`Derived`结构体嵌套了`Base`结构体,并添加了自己的成员变量`y`。我们也定义了一些接口函数来操作这些成员变量。`printX`函数是一个多态的函数,可以接受`Base`结构体的指针作为参数,实现了多态的特性。 希望这个示例代码可以帮助您更好地理解如何在C语言中实现封装、继承和多态的特性。如果还有其他问题,请随时询问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值