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

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

问题描述

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



限制:




  • 我必须使用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屋!

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