問題描述
對于必須處理分布在多個源文件和頭文件中的大量相互依賴的類的人,您建議的 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++類頭文件組織的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!