对于必须处理分布在多个源文件和头文件中的大量相互依赖的类的人,您建议的 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.这允许您打破循环头依赖关系.本质上,对于跨单独文件的循环依赖,您需要一个看起来像这样的文件依赖图:A.cxx 需要 A.h 和 B.hB.cxx 需要 A.h 和 B.hA.h 需要 B.hB.h 是独立的(并且前向声明在 A.h 中定义的类)foo.cxx, everything defined in there had better be declared in foo.h.#includes 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 requires A.h and B.hB.cxx requires A.h and B.hA.h requires B.hB.h is independent (and forward-declares classes defined in A.h)如果您的代码旨在成为其他开发人员使用的库,则需要采取一些额外的步骤:
If your code is intended to be a library consumed by other developers, there are some additional steps that are important to take:
include/ 和 src/ 子目录,其中 include/ 包含我所有的公共头文件,并且 src/ 有我所有的来源.和私人标题.include/ and src/ subdirectories in my C or C++ projects, where include/ has all of my public headers, and src/ 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++类头文件组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 C++ 中读取和操作 CSV 文件数据?How can I read and manipulate CSV file data in C++?(如何在 C++ 中读取和操作 CSV 文件数据?)
在 C++ 中,为什么我不能像这样编写 for() 循环:In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,为什么我不能像这样编写 for() 循环: for(
OpenMP 如何处理嵌套循环?How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
在循环 C++ 中重用线程Reusing thread in loop c++(在循环 C++ 中重用线程)
需要精确的线程睡眠.最大 1ms 误差Precise thread sleep needed. Max 1ms error(需要精确的线程睡眠.最大 1ms 误差)
是否需要“do {...} while ()"?环形?Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?环形?)