组织大型 C++项目
项目中的所有 C++ 代码是否应该封装到一个类中,并且 main 只需调用该类?或者应该在主函数中声明变量和类。
Should all C++ code in a project be encapsulated into a single class with main simply calling that class? Or should the main function declare variables and classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果你打算用 C++ 构建一个大型项目,你至少应该阅读 Large John Lakos 的 Scale C++ Software Design 对此进行了介绍。它有点旧,但听起来您可以从其中的基础知识中受益。
请记住,用任何语言构建大型系统都是一个挑战,需要技巧和纪律来防止它很快崩溃。不要掉以轻心。
也就是说,如果您对“大”的定义与我的不同,那么我可能会给您提供其他建议。我假设您正在谈论一个项目,其中在也包含“代码行”一词的句子中会提到“百万”一词。
If you are going to build a large project in C++, you should at the very least read Large Scale C++ Software Design by John Lakos about it. It's a little old but it sounds like you could benefit from the fundamentals in it.
Keep in mind that building a large scale system in any language is a challenge and requires skill and discipline to prevent it falling to pieces very quickly. Don't take it lightly.
That said, if your definition of "large" is different than mine then I may have alternative advice to give you. I'm assuming you're talking about a project where the word "million" will be mentioned in sentences that also contain the words "lines of code".
对于大型 C++ 项目,您应该创建许多类!
main 应该开始工作(也许做一些内务工作),然后调用一个类来启动系统的其余部分
for large C++ projects, you should create many classes!
main should just kick things off (maybe doing a few housekeeping things) and then calling into a class that will fire up the rest of the system
如果它是一个真正有意义的类,当然 - 但至少 IME,这是一个相当罕见的例外,而不是一般规则。
在这里,我假设您并不是真正的意思是所有代码都在一个类中,而是有一个顶级类,因此基本上所有
main
所做的就是实例化并使用它。反过来,该类可能会实例化并使用其他从属类。如果您的真正意思是“所有代码都应该包含在一个类中吗?”,那么答案几乎肯定是响亮的“不”,除了真正很小的项目。如果所有代码都在一个类中,那么 C++(以及大多数其他 OO 语言)中的大部分类设计都是完全没有意义的。
If it's a class that really makes sense, sure -- but at least IME, that's a fairly rare exception, not the general rule.
Here, I'm presuming that you don't really mean all the code is in one class, but that there's a single top-level class, so essentially all
main
does is instantiate and use it. That class, in turn, will presumably instantiate and use other subordinate classes.If you really mean "should all the code being contained in a single class?", then the answer is almost certainly a resounding "no", except for truly minuscule projects. Much of the design of classes in C++ (and most other OO languages) is completely pointless if all the code is in one class.
如果您可以将整个项目放在一个类中而不发疯,那么您对“大”的定义可能与这里大多数人的定义不同。这很好——只要记住,当您向人们询问“大型”C++ 项目时,他们会假设您正在谈论需要花费多个人年才能创建的东西。
也就是说,无论项目规模有多大,相同的封装原则都适用。将您的逻辑和数据分解为有意义且不太捆绑在一起的单元,然后围绕这些划分组织您的类。如果您发现自己复制粘贴代码,或者发现一个类过于依赖另一个类,请不要害怕尝试一种组织,然后将其重构为另一种组织。 (或者,如果您发现自己有太多的类,并且要创建许多对象来完成一项任务,那么单个对象对您来说会更干净、更容易。)
享受乐趣,不要害怕尝试一下。
If you can put your entire project in one class without going insane, your definition of "large" may be different than most people's here. Which is fine -- just keep in mind when you ask people about a "large" c++ project, they will assume you're talking about something that takes multiple person-years to create.
That said, the same principles of encapsulation apply no matter what the size of the project. Break your logic and data into units that make sense and are not too tied together and then organize your class(es) around those divisions. Don't be afraid to try one organization and then refactor it into another organization if you find yourself copy-pasting code, or if you find one class depending too heavily on another. (Or if you find yourself with too many classes and you're creating many objects to accomplish one task where a single object would be cleaner and easier on you.)
Have fun and don't be afraid to experiment a little.
在 C++ 中,无论大小,都应该避免将整个项目放入一个
类
中。您最多可以尝试将其放入 1 或 2 个命名空间
(可以跨文件拆分)。拥有多个类的优点是:
In C++ you should avoid putting entire project in one
class
, irrespective of big or small. At the max you can try putting it in 1 or 2namespace
(which can be split across the files).The advantage of having multiple classes are,
不!每个头文件/实现文件对应该代表一个类。将大型项目放在一个文件中肯定会导致灾难:该项目将变得难以维护并且编译将花费很长时间。将您的代码分解为适当大小的部分。
main 函数不应该声明类,而是它包含的文件(通常命名为 main.cpp、driver.cpp、projectname.cpp)应该使用 #include 指令来让编译器读取头文件中的声明。阅读 C++ 的单独编译模型以获取更多信息。
一些 C++ 新手发现编译模型 - 以及搞砸时生成的错误代码 - 难以理解或令人生畏,并认为它不值得而放弃。别让这成为你。了解如何正确组织您的代码。
No! Each header/implementation file pair should represent a single class. Placing a large project in one file is a surefire way to disaster: the project become unmaintainable and compiling will take ages. Break up your code in to appropriately sized pieces.
The main function should not declare the classes, rather, the file it contains (often named something like main.cpp, driver.cpp, projectname.cpp) should use #include directives to make the compiler read the declarations in header files. Read up on C++'s separate compilation model for more info.
Some newcomers to C++ find the compilation model - as well as error codes generated when you screw it up - incomprehensible or intimidating and give up thinking it's not worth it. Don't let this be you. Learn how to properly organize your code.