你如何在C中实现一个类? [英] How do you implement a class in C?
问题描述
限制:
- 我必须使用C而不是OOP
,因为我正在为
嵌入式系统编写代码,编译器和
预先存在的代码库位于C. - 没有动态内存分配
,因为我们没有足够的内存
来合理地假设我们不会运行如果我们开始动态分配
,那么
。 - 我们使用的编译器没有函数指针的问题
这取决于你想拥有的确切的面向对象功能集。如果你需要重载和/或虚拟方法的东西,你可能需要在结构中包含函数指针:
typedef struct {
float(* computeArea)(const ShapeClass * shape);
} ShapeClass;
float shape_computeArea(const ShapeClass * shape)
{
return shape-> computeArea(shape);
}
这将让你实现一个类,通过继承基类,并实现一个合适的功能:
typedef struct {
ShapeClass shape;
浮动宽度,高度;
} RectangleClass;
static float rectangle_computeArea(const ShapeClass * shape)
{
const RectangleClass * rect =(const RectangleClass *)shape;
return rect-> width * rect-> height;
}
这当然要求您还实现一个构造函数,确保函数指针正确设置。通常你会为实例动态分配内存,但是你也可以让调用者也这样做:
void rectangle_new(RectangleClass * rect)
{
rect-> width = rect-> height = 0.f;
rect-> shape.computeArea = rectangle_computeArea;
}
如果你想要几个不同的构造函数,你将不得不修饰功能名称,你不能有多个 rectangle_new()
function:
void rectangle_new_with_lengths(RectangleClass * rect,float width,float height)
{
rectangle_new(rect);
rect-> width = width;
rect-> height = height;
}
以下是一个基本示例,显示用法:
int main(void)
{
RectangleClass r1;
rectangle_new_with_lengths(& r1,4.f,5.f);
printf(rectangle r1的区域是%f units square\\\
,shape_computeArea(& r1));
return 0;
}
我希望这给你一些想法,至少。对于C中成功且丰富的面向对象的框架,请查看glib的 GObject 库。
另请注意,上面没有明确的类模型,每个对象都有自己的方法指针,比通常在C ++中找到的更有弹性。此外,它需要记忆。您可以通过将方法指针填充到类
结构中来避免这种情况,并为每个对象实例引用类创建一种方法。
Assuming I have to use C (no C++ or object oriented compilers) and I don't have dynamic memory allocation, what are some techniques I can use to implement a class, or a good approximation of a class? Is it always a good idea to isolate the "class" to a separate file? Assume that we can preallocate the memory by assuming a fixed number of instances, or even defining the reference to each object as a constant before compile time. Feel free to make assumptions about which OOP concept I will need to implement (it will vary) and suggest the best method for each.
Restrictions:
- I have to use C and not an OOP because I'm writing code for an embedded system, and the compiler and preexisting code base is in C.
- There is no dynamic memory allocation because we don't have enough memory to reasonably assume we won't run out if we start dynamically allocating it.
- The compilers we work with have no problems with function pointers
That depends on the exact "object-oriented" feature-set you want to have. If you need stuff like overloading and/or virtual methods, you probably need to include function pointers in structures:
typedef struct {
float (*computeArea)(const ShapeClass *shape);
} ShapeClass;
float shape_computeArea(const ShapeClass *shape)
{
return shape->computeArea(shape);
}
This would let you implement a class, by "inheriting" the base class, and implementing a suitable function:
typedef struct {
ShapeClass shape;
float width, height;
} RectangleClass;
static float rectangle_computeArea(const ShapeClass *shape)
{
const RectangleClass *rect = (const RectangleClass *) shape;
return rect->width * rect->height;
}
This of course requires you to also implement a constructor, that makes sure the function pointer is properly set up. Normally you'd dynamically allocate memory for the instance, but you can let the caller do that, too:
void rectangle_new(RectangleClass *rect)
{
rect->width = rect->height = 0.f;
rect->shape.computeArea = rectangle_computeArea;
}
If you want several different constructors, you will have to "decorate" the function names, you can't have more than one rectangle_new()
function:
void rectangle_new_with_lengths(RectangleClass *rect, float width, float height)
{
rectangle_new(rect);
rect->width = width;
rect->height = height;
}
Here's a basic example showing usage:
int main(void)
{
RectangleClass r1;
rectangle_new_with_lengths(&r1, 4.f, 5.f);
printf("rectangle r1's area is %f units square\n", shape_computeArea(&r1));
return 0;
}
I hope this gives you some ideas, at least. For a successful and rich object-oriented framework in C, look into glib's GObject library.
Also note that there's no explicit "class" being modelled above, each object has its own method pointers which is a bit more flexible than you'd typically find in C++. Also, it costs memory. You could get away from that by stuffing the method pointers in a class
structure, and invent a way for each object instance to reference a class.
这篇关于你如何在C中实现一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!