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

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

问题描述

假设我必须使用C(没有C ++或面向对象的编译器),并且我没有动态内存分配,我可以使用一些技术来实现一个类,还是一个类的很好的近似?将类隔离到单独的文件总是一个好主意?假设我们可以通过假设固定数量的实例来预分配内存,或者甚至在编译时间之前将每个对象的引用定义为常量。请自由地假设我需要实现哪些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;
float width,height;
} 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()函数:

  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's area is%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天全站免登陆