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

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

问题描述

我已经在头文件中看到了 struct 的完整定义和声明——一种方法比另一种方法有什么优势吗?

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

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

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

typedef struct s s_t;


要清楚,选项是头文件中的声明和类中的定义,或者头文件中的声明和定义.两者都应该产生相同的可用性,即使一个是通过链接,不是吗?


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.

推荐答案

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

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 .

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

Public structures should go in the .h file.

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

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