gpt4 book ai didi

c - Get and Set ("private") 在 C 中的缺点?

转载 作者:太空狗 更新时间:2023-10-29 16:11:51 25 4
gpt4 key购买 nike

我想在 C 中创建一个结构,任何人都不能直接访问它的数据,只能像在面向对象中那样通过 gets 和 set 访问。我的解决方案是这样的:

点.h

#ifndef POINT_H
#define POINT_H

typedef struct point Point;

Point *CreatePoint(int x, int y);

int Point_GetX(Point *point);
void Point_SetX(Point *point, int x);

int Point_GetY(Point *point);
void Point_SetY(Point *point, int y);

void DeletePoint(Point **p);

#endif /* POINT_H */

点.c

#include "Point.h"

struct point{
int x, y;
};

Point *CreatePoint(int x, int y){
Point *p = malloc(sizeof(Point));
if (p == NULL) return NULL;

p->x = x;
p->y = y;

return p;
}

int Point_GetX(Point *point){
return point->x;
}
void Point_SetX(Point *point, int x){
point->x = x;
}
int Point_GetY(Point *point){
return point->y;
}
void Point_SetY(Point *point, int y){
point->y = y;
}

void DeletePoint(Point **p){
if (*p != NULL){
free(*p);
*p = NULL;
}
}

主.c

#include <stdio.h>
#include <stdlib.h>
#include "Point.h"

int main(){
Point *p = CreatePoint(2, 6);
if (p == NULL) return EXIT_FAILURE;
/*
p->x = 4; "error: dereferencing pointer
p->y = 9; to incomplete type"
*/
Point_SetX(p, 4);
Point_SetY(p, 9);

printf("POINT(%d,%d)", Point_GetX(p), Point_GetY(p));

DeletePoint(&p);

return EXIT_SUCCESS;
}

当然,我绝不会为了一个简单的观点而对所有这一切进行说明,但这就是我的想法。我想知道这样做会出现什么问题,我是否应该这样做,以及是否可以这样做,如果这不是一个聪明的方法(我应该只选择 c++ xD)。这样做的原因是我正在做一个小项目,但稍后我可能会更改一些数据结构和算法,所以如果我这样做,我只需要更改 Point.c 在这种情况下,而不是我的每个地方会做

point->x = new_x

例如。

基本上,我是在尝试做 C++ 做的事情吗?或者可以在 C 中执行此操作吗?或者没有,有缺点吗?或者这根本不是 C 的本意?哈哈

最佳答案

My solution was something like [this]

这是 C 中用于信息隐藏的经典解决方案。唯一缺少的是 DestroyPoint,一个用于释放您使用 malloc 分配的点结构的函数。

I want to know what could go wrong by doing this, whether I should be doing this or not, and if it's OK to do this.

这是一种安全的方法,只要您接受它的缺点,如下所述。

[is there] a disadvantage?

是的,有:这种方法仅限于动态分配您隐藏其内部的数据结构。你不能这样做:

Point p; // This fails, because `Point` is a forward declaration.
int x = Point_GetX(&p);

同样,Point 的数组也是禁止的;将 Point 嵌入其他 struct 也是不可能的。

Am I trying to do what C++ does?

不是真的。您的方法类似于 Objective-C,因为 C++ 没有仅在动态存储中分配对象的限制,而 Objective-C 有。不过,这种限制似乎不会给 Objective-C 程序员带来太多问题。

关于c - Get and Set ("private") 在 C 中的缺点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29584795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com