src/ folder structure in C++?

前端 未结 6 1085
离开以前
离开以前 2021-02-02 00:01

i\'m coming into C++ from Java/AS3-land, and i\'m used to the package-cum-folder structure for my classes. and i like it.

i understand the very basics of namespaces in

6条回答
  •  春和景丽
    2021-02-02 00:43

    There's no reason not to divide your source code into different directories; it makes sense if there are many files and clear logical groupings.

    It is not necessary to create a distinct file for each small class though - in large projects, that tends to slow compilation (as the implementation files often have to include a lot of the same headers just to compile their couple dozen lines).

    As well as the use of namespaces for reflecting the logical divisions in the code, the exact thresholds at which code is subdivided into further namespaces tends to be driven by some other forces, for example:

    • factors suggesting use of more namespaces
      • very volatile code (often edited, constant additional/changed identifier use, often short and/or common words)
      • more developers
    • factors reducing the need for namespaces
      • tight coordination by a central body
      • planned formal releases with thorough checks for conflicts

    Namespaces can also be used as a way to allow easy switching between alternative implementations (e.g. different versions of a protocol, thread-safe versus unsafe support functions, OS-specific implementations), so sometimes catering for such needs involves use of distinct namespaces.

    It can definitely be painful digging through unintuitive and/or deeply nested namespaces to reach the variables you want, and "using namespace" is less effective if you're likely to need to use several that define the same identifiers anyway, but can suit more modal code that tends to use one or the other namespace more heavily at a time.

    So, you may want to consider these factors when deciding whether to put each folder's code (or some other logically distinct group) into distinct namespaces.

提交回复
热议问题