久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++類頭文件組織

C++ class header files organization(C++類頭文件組織)
本文介紹了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.hB.h
    • B.cxx 需要 A.hB.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 in foo.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 #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.h
      • B.cxx requires A.h and B.h
      • A.h requires B.h
      • B.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:

      • 如有必要,請使用私有標頭"的概念.也就是說,幾個源文件需要的頭文件,但公共接口從來不需要.這可以是包含常見內聯函數、宏或內部常量的文件.
      • 在文件系統級別將公共接口與私有實現分開.我傾向于在我的 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/ 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模板網!

      【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 国产成人在线观看免费网站 | 国产天堂在线观看 | 免费看片网站91 | 性久久久久久久 | 久久久麻豆 | 成人午夜视频在线观看 | 日本高清www| 亚洲三级小说 | 欧美日韩一二区 | 成人在线黄色 | 久久九| 欧美日韩中文在线 | 黄色片网站在线观看 | 色综合天天综合网天天狠天天 | 国产午夜三级 | 青青草国产成人av片免费 | 色爱av| 一区二区三区亚洲 | 高清一区二区三区 | 在线免费av网站 | 狠狠干伊人 | 九九久久免费视频 | 九九热这里 | 国产精品久久久久久久午夜 | 日韩免费在线观看视频 | av手机天堂网 | 国产精品久久久久永久免费看 | 国产激情视频 | 日本不卡高字幕在线2019 | 综合久久综合 | 日韩成人中文字幕 | 深夜福利视频网站 | 日韩视频在线观看免费 | 久久精品2 | 亚洲综合一区二区 | 日韩在线免费视频 | 久久综合久 | 日韩精品视频在线免费观看 | 欧美精品一二三 | 少妇一级淫免费观看 | a视频在线免费观看 |