C++ class header files organization(C++类头文件组织)
问题描述
对于必须处理分布在多个源文件和头文件中的大量相互依赖的类的人,您建议的 C++ 编码和文件组织指南是什么?
What are the C++ coding and file organization guidelines you suggest for people who have to deal with lots of interdependent classes spread over several source and header files?
我在我的项目中遇到了这种情况,解决跨多个头文件的类定义相关错误已经变得非常令人头疼.
I have this situation in my project and solving class definition related errors crossing over several header files has become quite a headache.
推荐答案
一些通用指南:
- 将您的接口与实现配对.如果您有
foo.cxx
,那么在其中定义的所有内容最好在foo.h
中声明. - 确保每个头文件#include 独立编译所需的所有其他必要的头文件或前向声明.
- 抵制创建一切"标题的诱惑.他们总是在路上遇到麻烦.
- 将一组相关(和相互依赖)的功能放入一个文件中.Java 和其他环境鼓励每个文件一个类.对于 C++,您通常希望每个文件有一组 类.这取决于您的代码结构.
- 尽可能优先使用前向声明而不是
#include
.这允许您打破循环头依赖关系.本质上,对于跨单独文件的循环依赖,您需要一个看起来像这样的文件依赖图:A.cxx
需要A.h
和B.h
B.cxx
需要A.h
和B.h
A.h
需要B.h
B.h
是独立的(并且前向声明在A.h
中定义的类)
- Pair up your interfaces with implementations. If you have
foo.cxx
, everything defined in there had better be declared infoo.h
. - Ensure that every header file #includes all other necessary headers or forward-declarations necessary for independent compilation.
- Resist the temptation to create an "everything" header. They're always trouble down the road.
- Put a set of related (and interdependent) functionality into a single file. Java and other environments encourage one-class-per-file. With C++, you often want one set of classes per file. It depends on the structure of your code.
- Prefer forward declaration over
#include
s whenever possible. This allows you to break the cyclic header dependencies. Essentially, for cyclical dependencies across separate files, you want a file-dependency graph that looks something like this:A.cxx
requiresA.h
andB.h
B.cxx
requiresA.h
andB.h
A.h
requiresB.h
B.h
is independent (and forward-declares classes defined inA.h
)
如果您的代码旨在成为其他开发人员使用的库,则需要采取一些额外的步骤:
If your code is intended to be a library consumed by other developers, there are some additional steps that are important to take:
- 如有必要,请使用私有标头"的概念.也就是说,几个源文件需要的头文件,但公共接口从来不需要.这可以是包含常见内联函数、宏或内部常量的文件.
- 在文件系统级别将公共接口与私有实现分开.我倾向于在我的 C 或 C++ 项目中使用
include/
和src/
子目录,其中include/
包含我所有的公共头文件,并且src/
有我所有的来源.和私人标题.
- If necessary, use the concept of "private headers". That is, header files that are required by several source files, but never required by the public interface. This could be a file with common inline functions, macros, or internal constants.
- Separate your public interface from your private implementation at the filesystem level. I tend to use
include/
andsrc/
subdirectories in my C or C++ projects, whereinclude/
has all of my public headers, andsrc/
has all of my sources. and private headers.
我建议您找一本 John Lakos 的书大规模 C++ 软件设计.这是一本相当厚重的书,但如果您只是浏览他关于物理建筑的一些讨论,您会学到很多东西.
I'd recommend finding a copy of John Lakos' book Large-Scale C++ Software Design. It's a pretty hefty book, but if you just skim through some of his discussions on physical architecture, you'll learn a lot.
这篇关于C++类头文件组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++类头文件组织
基础教程推荐
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01