所以说我正在编写一个链接列表,在我的标题文件中我有:
struct Node {
LIST_TYPE value;
Node* next;
Node* prev;
};
struct List{
int size;
Node* head;
Node* tail;
void (*list_print_function)(void*);
};
,但我不希望客户能够看到节点结构。静态修饰符是正确的方法吗?关于静态结构的文档不多。
您想要的是 opaque 结构,您可以通过在标题文件中声明struct
来做到这一点,该文件称为 fortht offort oferation 是仅在实现文件中定义它,就像您给出函数原型时一样,但您不给出定义。
您可以提供访问器功能,以允许库用户使用struct
,示例
data-type.h
struct Struct;
void struct_set_value(struct Struct *struct, int value);
int struct_get_value(const struct Struct *const instance);
data-type.c
struct Struct
{
int value;
};
void struct_set_value(struct Struct *instance, int value)
{
instance->value = value;
}
int struct_get_value(const struct Struct *const instance)
{
return instance->value;
}
您可以,然后将.h
文件提供给库用户和编译对象,以便它们可以链接到它。
只要您只使用Node
的指针,就可以将struct
定义本身隐藏给库的用户。您唯一必须在列表定义之前看到的东西是
typedef struct Node Node;
struct Node
本身是您的struct
类型的正向声明。需要typedef
才能在没有struct
的情况下使用Node
。