你如何在 C 中实现一个类? [英] How do you implement a class in C?

查看:25
本文介绍了你如何在 C 中实现一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我必须使用 C(没有 C++ 或面向对象的编译器)并且我没有动态内存分配,我可以使用哪些技术来实现一个类,或者一个类的一个很好的近似?将类"隔离到单独的文件总是一个好主意吗?假设我们可以通过假设固定数量的实例来预分配内存,甚至在编译之前将每个对象的引用定义为常量.随意假设我需要实现哪个 OOP 概念(它会有所不同),并为每个概念建议最佳方法.

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.

限制:

  • 我必须使用 C 而不是 OOP因为我正在为一个嵌入式系统,以及编译器和预先存在的代码库是用 C 编写的.
  • 没有动态内存分配因为我们没有足够的内存合理地假设我们不会用完如果我们开始动态分配
  • 我们使用的编译器在函数指针方面没有问题

推荐答案

这取决于您想要拥有的确切面向对象"功能集.如果您需要重载和/或虚拟方法之类的东西,您可能需要在结构中包含函数指针:

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;
}

如果你想要几个不同的构造函数,你必须修饰"函数名,你不能有多个rectangle_new()函数:

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
", shape_computeArea(&r1));
  return 0;
}

我希望这至少能给你一些想法.要获得成功且丰富的 C 面向对象框架,请查看 glib 的 GObject 库.

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.

另请注意,上面没有建模的明确类",每个对象都有自己的方法指针,这比您通常在 C++ 中找到的要灵活一些.此外,它会消耗内存.您可以通过在 class 结构中填充方法指针来避免这种情况,并为每个对象实例发明一种引用类的方法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆