使用结构在C中模拟类 [英] Emulating Classes in C using Structs

查看:89
本文介绍了使用结构在C中模拟类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只能在比赛中使用C,并且我需要模拟类。我正在尝试构造一个简单的点类,该类可以返回并设置点的X和Y坐标。但是,下面的代码返回错误,例如未知类型名称点,期望的标识符或(和期望的参数声明符。这些错误是什么意思?我该如何纠正它们?这是编写伪类?

I am constrained to using C for a competition and I have a need to emulate classes. I am trying to construct a simple "point" class that can return and set the X and Y coordinates of a point. Yet, the below code returns errors such as "unknown type name point", "expected identifier or (" and "expected parameter declarator." What do these errors mean? How do I correct them? Is this the correct approach to writing a "pseudo-class"?

typedef struct object object, *setCoordinates;

struct object {
    float x, y;
    void (*setCoordinates)(object *self, float x, float y);
    void (*getYCoordinate)(object *self);
    void (*getXCoordinate)(object *self);
};

void object_setCoordinates(object *self, float x, float y){
    self->x = x;
    self->y = y;
}

float object_getXCoordinate(object *self){
    return self->x;
}

float object_getYCoordinate(object *self){
    return self->y;
}

object point;
point.setCoordinates = object_setCoordinates;
point.getYCoordinate = object_getYCoordinate;
point.getXCoordinate = object_getXCoordinate;

point.setCoordinates(&point, 1, 2);
printf("Coordinates: X Coordinate: %f, Y Coordinate: %f", point.getXCoordinate, point.getYCoordinate);

参考:
1. C-结构内部的函数
2. 如何在C语言中实现类?

推荐答案

您可以更好地实现它,如下所示:

You would do much better to implement it as follows:

#include <stdio.h>

struct point {
    float x;
    float y;
};

void point_setCoordinates(struct point *self, float x, float y){
    self->x = x;
    self->y = y;
}

float point_getXCoordinate(struct point *self){
    return self->x;
}

float point_getYCoordinate(struct point *self){
    return self->y;
}

int main(void) {
    struct point my_point;

    point_setCoordinates(&my_point, 1, 2);

    printf("Coordinates: X Coordinate: %f, Y Coordinate: %f\n",
           point_getXCoordinate(&my_point),
           point_getYCoordinate(&my_point));

    return 0;
}

一些注意事项:


  • 正如@Olaf指出的那样,永远不要typedef指针-它隐藏了您的意图,使事情变得不清楚。是的,它遍及所有较差的API(例如Windows),但会降低可读性。

  • 您真的不需要这些功能等同于虚拟功能...只要有一个在 point 'thing'上调用的 point _ *()函数集。

  • 不要将名称混为一谈...如果是X,Y点,则称其为-不是对象(这是一个非常通用的概念)。

  • 您需要调用函数...在调用 printf()时,您使用了 point.getXCoordinate -说您拿了它的地址,并要求 printf()显示它,就好像它是 float

  • 您可能会开始怀疑,为什么要关心调用一个函数来访问透明结构内的变量...参见下文。

  • As @Olaf has pointed out, never typedef a pointer - it hides your intent and makes things unclear. Yes, it's all over poor APIs (e.g: Windows), but it reduces readability.
  • You really don't need these functions to be the equivalent to virtual functions... just have a set of point_*() functions that you call on the point 'thing'.
  • Don't confuse things with poor names... if it's an X,Y point, then call it such - not an object (which is a very generic concept).
  • You need to call functions... in your call to printf() you used point.getXCoordinate - that is to say you took it's address and asked printf() to display it as though it were a float
  • You might start to wonder why you'd care about calling a function to get access to a variable that is inside a transparent struct... See below.

许多库/ API提供不透明的数据类型。这意味着您可以对事物进行处理……但是您不知道事物中存储了什么。然后,该库为您提供访问功能,如下所示。这是我建议您处理这种情况的方式。

Many libraries / APIs provide opaque datatypes. This means that you can get a 'handle' to a 'thing'... but you have no idea what's being stored within the 'thing'. The library then provides you with access functions, as shown below. This is how I'd advise you approach the situation.

别忘了释放内存!

我在下面实现了一个示例。

I've implemented an example below.

point.h

#ifndef POINT_H
#define POINT_H

struct point;

struct point *point_alloc(void);
void point_free(struct point *self);

void point_setCoordinates(struct point *self, float x, float y);
float point_getXCoordinate(struct point *self);
float point_getYCoordinate(struct point *self);

#endif /* POINT_H */

point.c

#include <stdlib.h>
#include <string.h>

#include "point.h"

struct point {
    float x;
    float y;
};

struct point *point_alloc(void) {
    struct point *point;

    point = malloc(sizeof(*point));
    if (point == NULL) {
        return NULL;
    }

    memset(point, 0, sizeof(*point));

    return point;
}

void point_setCoordinates(struct point *self, float x, float y) {
    self->x = x;
    self->y = y;
}

float point_getXCoordinate(struct point *self) {
    return self->x;
}

float point_getYCoordinate(struct point *self) {
    return self->y;
}

void point_free(struct point *self) {
    free(self);
}

main.c

#include <stdio.h>

#include "point.h"

int main(void) {
    struct point *point;

    point = point_alloc();

    point_setCoordinates(point, 1, 2);

    printf("Coordinates: X Coordinate: %f, Y Coordinate: %f\n",
           point_getXCoordinate(point),
           point_getYCoordinate(point));

    point_free(point);

    return 0;
}

这篇关于使用结构在C中模拟类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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