简体   繁体   English

c typedef(ed) 不透明指针

[英]c typedef(ed) opaque pointer

I've defined an opaque structure and related APIs like this:我已经定义了一个不透明的结构和相关的 API,如下所示:

typedef struct foo foo;
foo *create_foo(...);
delete_foo(foo *f);

I am not able to define the structure in my c file.我无法在我的 c 文件中定义结构。 Gives redefinition error.给出重新定义错误。

typedef struct foo {
   int implementation;
}foo;

I am able to use foo in c file without typedef but I want the typedef (ie use it directly as foo*).我可以在没有 typedef 的 c 文件中使用 foo 但我想要 typedef(即直接将它用作 foo*)。 Is there a way?有办法吗?

You already have the typedef in your header, so include that and define struct foo in the implementation without the typedef .您的标头中已经有typedef ,因此在没有typedef的实现中包含它并定义struct foo

foo.h : foo.h :

typedef struct foo foo;
foo *create_foo(...);
delete_foo(foo *f);

foo.c : foo.c :

#include <foo.h>

struct foo { int implementation; };
/* etc. */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM