结构定义应该放在.h或.c文件中吗? [英] Should struct definitions go in .h or .c file?

查看:255
本文介绍了结构定义应该放在.h或.c文件中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在头文件中看到了 struct 的全部定义,并且声明中只有一个方法比另一个方法有什么优势?



如果它有所不同,我通常在 .h



中键入一个结构

  typedef struct s s_t; 






编辑



明确地说,这些选项是头文件中的声明和类中的定义,或者是头文件中的声明和定义。这两个应该导致相同的可用性,即使一个是联系,不应该吗?






我看到很多差不多重复,例如此处,但没有完全匹配。如果我在这方面有错误,请纠正我。 解决方案

该文件的私有结构应放在.c文件中,在.h文件中声明它们是否被.h中的任何函数使用。



公共结构应该放在.h文件中。


I've seen both full definitions of structs in headers and just declarations—is there any advantage to one method over the other?

If it makes a difference, I usually typedef a struct like so in the .h

typedef struct s s_t;


Edit

To be clear, the options are declaration in the header file and definition in the class, or both declaration and definition in the header file. Both should result in the same usability, even if one is by linkage, shouldn't they?


I see many almost duplicates, e.g. here but no exact matches. Please correct me if I'm wrong in this regard.

解决方案

Private structures for that file should go in the .c file, with a declaration in the .h file if they are used by any functions in the .h .

Public structures should go in the .h file.

这篇关于结构定义应该放在.h或.c文件中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆