久久久久久久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不卡| 性色av香蕉一区二区 | 国产激情99 | 女女百合av大片一区二区三区九县 | 久久综合99 | 中文字幕亚洲精品 | 国产一区二区三区四区五区3d | 黄色在线观看网站 | 国产在线观看网站 | 国产区精品在线观看 | 7777在线视频免费播放 | 日韩在线一区二区 | 日本福利在线观看 | 中文字幕在线欧美 | 国产精品特级毛片一区二区三区 | 夜色www国产精品资源站 | 亚洲免费精品 | 91一区二区 | 91亚洲视频在线 | 久久国产精品一区 | 超碰在线人 | 黄一级| 亚洲国产成人av好男人在线观看 | 一级欧美一级日韩片 | 日本电影韩国电影免费观看 | 在线观看亚洲专区 | 99精品久久| 国产精品99久久久久久久久久久久 | 久久久123| 日本黄色一级视频 | 国产成人精品午夜 | 视频在线一区 | 成人福利在线 | av一区二区在线观看 | 天天操操 | 欧美亚洲一区二区三区 | 中文字幕乱码一区二区三区 | 日韩免费一区 | 成人免费xxxxx在线视频 |