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

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

问题描述

假设我要使用C(没有C ++或面向对象的编译器),我没有动态内存分配,都有些什么技术,我可以用它来实现一个类或一类的一个很好的近似?是否总是一个好主意,阶级隔离到一个单独的文件?假设我们可以preallocate存储器通过假设实例的固定数量,或者甚至限定为恒定之前编译时间的参考的每个对象。随意作出假设哪些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
    因为我在写code为
    嵌入式系统,和编译器和
    preexisting code碱基是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\n", shape_computeArea(&r1));
  return 0;
}

我希望这给你一些想法,至少。在C成功和丰富的面向对象的框架,看看巧舌如簧的图形对象库。

另外请注意,有没有明确的阶级之上建模,每个对象都有自己的方法指针是多一点灵活的比你通常会发现在C ++中。此外,它的成本存储器。你可以通过在结构馅方法指针从那个路程,创造一种让每一个对象实例的引用类。

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天全站免登陆