使用 Structs 模拟 C 中的类

2 c struct class

我被限制在比赛中使用 C,我需要模拟课程。我正在尝试构建一个简单的“点”类,它可以返回并设置一个点的 X 和 Y 坐标。然而,下面的代码返回诸如“未知类型名称点”、“预期标识符或(”和“预期参数声明符”等错误。这些错误是什么意思?我该如何纠正它们?这是编写“的正确方法”伪类”?

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);
Run Code Online (Sandbox Code Playgroud)

参考: 1. C - struct 内的函数 2.如何在 C 中实现类?

Att*_*tie 5

你会做得更好,如下所示:

#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;
}
Run Code Online (Sandbox Code Playgroud)

需要注意的几点:

  • 正如@Olaf 指出的那样,永远不要对指针进行 typedef - 它隐藏了您的意图并使事情变得不清楚。是的,它与糟糕的 API(例如:Windows)有关,但它降低了可读性。
  • 你真的不需要这些函数等同于虚函数……只要有一组point_*()你在point“事物”上调用的函数。
  • 不要将事物与糟糕的名称混淆......如果它是一个 X,Y 点,那么就这样称呼它 - 而不是一个对象(这是一个非常通用的概念)。
  • 您需要通话功能......在调用printf()你用point.getXCoordinate-也就是说,你把它的地址和要求printf(),以显示它,就好像它是一个float
  • 您可能会开始想知道为什么您会关心调用函数来访问透明结构中的变量……见下文。

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

不要忘记释放内存!

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

点.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 */
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

主文件

#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;
}
Run Code Online (Sandbox Code Playgroud)