怎么使用C语言实现一个简单的类

听到一哥们碰到一个面试题,说使用C模拟一个类。我们知道C中是没有类的概念的,那我们怎么使用C模拟类呢?其实我们的办法并不多,宏定义可能会是一个不错的选择,下面是简单的尝试。

#define Struct_S \
	int a;\
	int b;\
	int c
	

struct S
{
	Struct_S;
};
	

#define Struct_H \
	Struct_S; \
	float e; \
	float f
	
struct H
{
	Struct_H;
};	


//成员函数
typedef struct S  S;
typedef struct H  H;

static  void  s_mem_function(S *obj_s)
{
	
}

static  void  h_mem_function(H *obj_h)
{
	
}

上面是简单的模拟了继承和成员函数的实现。其实,对于成员函数,也可以进一步模拟函数表,但是那样可能会显得啰嗦不自然,具体如下:

//函数表
struct FuncTable_S
{
		void (*s_mem_function_1)(S *obj_s);
		void (*s_mem_function_2)(S *obj_s);
}

struct S 
{
	Struct_S;
	
	struct FuncTable_S  funct_s;
};

这样以来调用的时候,会出现s->functt_s->s_function_1多重指定的问题,看着别扭。不如直接使用上面的形式方便。

其实能使用C的地方绝大多数时候都能使用C++,与其使用C去模拟C++现实类的特性,大多数时候还不如直接使用C++呢。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
装饰模式是一种结构性设计模式,它允许您通过将对象包装在具有新行为和责任的装饰器对象中来动态地修改对象的行为。使用C语言实现装饰模式时,您需要定义一个基本的接口或抽象基,该接口或基表示被装饰的对象。然后,您需要定义一个装饰器,该具有与基本接口或基相同的接口,并具有一个指向基本对象的指针,以便可以对其进行装饰。 下面是一个使用C语言实现装饰模式的示例代码: ``` #include <stdio.h> /* 基本接口或抽象基 */ typedef struct Component { void (*operation)(struct Component*); } Component; /* 具体对象 */ typedef struct ConcreteComponent { Component base; int state; } ConcreteComponent; void ConcreteComponent_operation(Component* component) { ConcreteComponent* self = (ConcreteComponent*) component; printf("ConcreteComponent_operation: state=%d\n", self->state); } /* 装饰器 */ typedef struct Decorator { Component* component; void (*operation)(Component*); } Decorator; void Decorator_operation(Component* component) { Decorator* self = (Decorator*) component; self->component->operation(self->component); } /* 具体装饰器 */ typedef struct ConcreteDecorator { Decorator base; int addedState; } ConcreteDecorator; void ConcreteDecorator_operation(Component* component) { ConcreteDecorator* self = (ConcreteDecorator*) component; printf("ConcreteDecorator_operation: addedState=%d\n", self->addedState); Decorator_operation(self->base.component); } int main() { /* 创建具体对象 */ ConcreteComponent concreteComponent; concreteComponent.base.operation = ConcreteComponent_operation; concreteComponent.state = 42; /* 创建具体装饰器对象 */ ConcreteDecorator concreteDecorator; concreteDecorator.base.component = (Component*) &concreteComponent; concreteDecorator.base.operation = Decorator_operation; concreteDecorator.addedState = 1337; /* 调用被装饰对象的操作 */ concreteComponent.base.operation((Component*) &concreteComponent); /* 调用装饰后的对象的操作 */ concreteDecorator.base.operation((Component*) &concreteDecorator); return 0; } ``` 在示例代码中,我们定义了一个基本接口或抽象基 `Component`,表示被装饰的对象,以及一个具体对象 `ConcreteComponent`,它实现了基本接口并具有一些状态。然后,我们定义了一个装饰器 `Decorator`,它具有一个指向基本对象的指针,并实现了基本接口。最后,我们定义了一个具体装饰器 `ConcreteDecorator`,它具有自己的状态并实现了基本接口,但是它还调用基本对象的操作。 在主函数中,我们首先创建一个具体对象 `concreteComponent`,然后创建一个具体装饰器对象 `concreteDecorator`,将其包装在 `concreteComponent` 上,并且调用了原对象和装饰后的对象的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值