C 中的不透明(抽象)数据类型
文件 api.h
#include <stdio.h>
#ifndef API
#define API
struct trytag;
typedef struct trytag try;
void trial (try *);
#endif
文件 core.h
#ifndef CORE
#define CORE
struct trytag
{
int a;
int b;
};
#endif
文件 func.c
#include "api.h"
#include "core.h"
void trial (try *tryvar)
{
tryvar->a = 1;
tryvar->b = 2;
}
文件 main.c
#include "api.h"
int main ()
{
try s_tryvar;
trial(&s_tryvar);
printf("a = %d\nb = %d\n", s_tryvar.a, s_tryvar.b);
}
当我编译时,我得到:
main.c:5: error: storage size of ‘s_tryvar’ isn’t known
如果我在 main.c
中包含 core.h
这个错误不会不会出现,因为 try 是在 core.h 中定义的。但我希望将结构 try
隐藏到 main.c
中 - 它不应该知道 try
结构的成员。我缺少什么?
File api.h
#include <stdio.h>
#ifndef API
#define API
struct trytag;
typedef struct trytag try;
void trial (try *);
#endif
File core.h
#ifndef CORE
#define CORE
struct trytag
{
int a;
int b;
};
#endif
File func.c
#include "api.h"
#include "core.h"
void trial (try *tryvar)
{
tryvar->a = 1;
tryvar->b = 2;
}
File main.c
#include "api.h"
int main ()
{
try s_tryvar;
trial(&s_tryvar);
printf("a = %d\nb = %d\n", s_tryvar.a, s_tryvar.b);
}
When I compile, I get:
main.c:5: error: storage size of ‘s_tryvar’ isn’t known
If I include core.h
in main.c
this error doesn't come as try is defined in core.h
. But I want the structure try
to be hidden to main.c
— it should not know the members of try
structure. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你想做的事情是不可能的。编译器需要知道
try
结构有多大才能编译main.c
。如果您确实希望它是不透明的,请创建一个通用指针类型,而不是直接在main()
中声明变量,而是创建alloc_try()
和free_try ()
函数来处理创建和删除。像这样的:
api.h:
core.h:
func.c:
main.c:
I don't think what you're trying to do is possible. The compiler needs to know how big a
try
structure is to compilemain.c
. If you really want it to be opaque, make a generic pointer type, and instead of declaring the variable directly inmain()
, makealloc_try()
andfree_try()
functions to handle the creation and deletion.Something like this:
api.h:
core.h:
func.c:
main.c:
想想不透明的 FILE 结构在 C 中是如何工作的。您只使用指针,并且需要像 fopen() 这样的函数来创建实例,以及像 fclose() 这样的函数来处理它。
Think how the opaque FILE structure works in C. You only work with pointers, and you need a function like fopen() to create an instance, and a function like fclose() to dispose of it.
问题出在main.c中,编译器没有看到
struct try
的定义。因此,编译器仅限于使用指向struct try
的指针。您想要做的是向 API 添加两个新函数:
这些函数将分别调用 malloc 和 free。
如果您不想将您的结构限制为仅允许在堆上使用,那么您将不得不放弃使其不透明。
The problem is in main.c, the compiler hasn't seen the definition of
struct try
. Because of that, the compiler is limited to using pointers tostruct try
.What you want to do is add two new functions to your API:
These functions will call malloc and free respectively.
If you don't want to limit your structure to only being allowed on the heap, you are going to have to give up on making it opaque.
有一种方法可以做一些技术上不完全是您所要求的事情,但应该达到相同的目的,即在支持非堆分配的同时保持结构不透明。
在 api.h 中,您按如下方式声明不透明结构:
如果您想要比这更不透明,您可以计算任何支持的平台所需的结构的最大大小,并使用:
然后您的 api.h 函数声明将看起来就像:
你的函数代码将如下所示:
你的 main.c 将如下所示:
There is a way to do something that technically is not exactly what you are asking for, but should serve the same purpose of keeping your structure opaque while supporting non-heap allocation.
in api.h, you state an opaque structure as follows:
if you wanted to be more opaque than that, you could calculate the maximum size of the structure required across any supported platform, and use:
Then your api.h function declarations would look like:
and your function code would look like:
and your main.c would look like: