C++ 是否可以在一个 .cpp 文件中定义多个类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1861679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to define multiple classes in just one .cpp file?
提问by Chad
I'm working on a project for school and the instructor insists that all code go into one .cpp file (for easier grading on his part). I would like to define multiple classes within this file. Will I run into any problems in doing this?
我正在为学校做一个项目,讲师坚持将所有代码放入一个 .cpp 文件中(以便他更轻松地评分)。我想在这个文件中定义多个类。我这样做会遇到任何问题吗?
回答by RED SOFT ADAIR
There is no rule you have to follow (like in java). You're free to place and name classes in however named files you like (besides the suffix).
没有必须遵循的规则(就像在 Java 中一样)。您可以随意在您喜欢的命名文件中放置和命名类(除了后缀)。
However its another question if that is good practices (its not!).
然而,这是另一个问题,如果这是好的做法(不是!)。
回答by Brian R. Bondy
Yes, you can. This is easily verifiable.
是的你可以。这很容易验证。
class C
{
};
class D
{
};
int main(int argc, char**argv)
{
return 0;
}
回答by aJ.
There is no problem in writing multiple classes into single file. It is only matter of maintenance style.
将多个类写入单个文件没有问题。这只是维护方式的问题。
回答by Carl Seleborg
You can perfectly have several classes declared and defined in the same file.
您可以完美地在同一个文件中声明和定义多个类。
You have to be careful, though, when a class depends on another which comes later. If a class A is to be a member of another class B, the compiler needs its complete declaration (so that it knows its size), and you need to put it higher up in the file. If this member is just a pointer however (which has a size independent of the size of the class pointed to), a simple forward-declaration of the pointed-to class is enough.
但是,当一个类依赖于另一个稍后出现的类时,您必须小心。如果一个类 A 是另一个类 B 的成员,编译器需要它的完整声明(以便它知道它的大小),并且您需要将它放在文件中的更高位置。然而,如果这个成员只是一个指针(它的大小与所指向的类的大小无关),那么一个简单的指向类的前向声明就足够了。
Note that with the #include
mechanism, that's pretty much what happens anyway: the preprocessor will "copy-paste" all included files into the file being compiled. To the compiler, it's just the same.
请注意,使用该#include
机制,无论如何都会发生这种情况:预处理器会将所有包含的文件“复制粘贴”到正在编译的文件中。对于编译器来说,它是一样的。
回答by Edward Loper
Typically, you should only put multiple classes in a single file if...
通常,您应该只将多个类放在一个文件中,如果...
The classes are very tightly linked. E.g., if a class defines its own iterator, then it might be appropriate to put that iterator class in the same file as the class that it's used to iterate over.
One of the classes is for public consumption, and the remaining classes are used to implement it. E.g., this will apply if you use the "pimpl" idiom, where the only member that the public class contains is a pointer to the private class. In this case, it may be appropriate to put the private/hidden classes in the source file corresponding to the public class that uses them.
课程之间的联系非常紧密。例如,如果一个类定义了它自己的迭代器,那么将该迭代器类与它用来迭代的类放在同一个文件中可能是合适的。
其中一个类用于公共消费,其余类用于实现它。例如,如果您使用“pimpl”习语,这将适用,其中公共类包含的唯一成员是指向私有类的指针。在这种情况下,将私有/隐藏类放在与使用它们的公共类相对应的源文件中可能是合适的。
In both cases, the decision of whether to put the "public helper" (e.g. iterator) and "private helper" (e.g. pimpl) classes in the same source file or a different source file should be made once for an entire project, and followed consistently.
在这两种情况下,应该为整个项目一次决定是否将“公共帮助程序”(例如迭代器)和“私有帮助程序”(例如 pimpl)类放在同一个源文件或不同的源文件中,并遵循始终如一。
回答by Liz Albin
You won't have any trouble defining multiple classes in a single file.
在单个文件中定义多个类不会有任何问题。
It isn't especially good practice for production systems, but that's not an issue for homework
对于生产系统来说,这不是特别好的做法,但这对于家庭作业来说不是问题
回答by RC.
Yes you can. This must be supported because otherwise you wouldn't be able to create nested classes.
是的你可以。这必须得到支持,否则您将无法创建嵌套类。
class outside
{
public:
class nested
{
public:
static int x;
static int y;
int f();
int g();
};
};
回答by gorsky
Just scalability/maintainability issues ;) There's no strict rules about it.
只是可扩展性/可维护性问题;) 对此没有严格的规定。
回答by arhuaco
No. You will not run into trouble. In C++ you can define multiple classes inside of a single file.
不会。您不会遇到麻烦。在 C++ 中,您可以在单个文件中定义多个类。
回答by progician
The question is coming from the java/C# style source representation. In C++ the source code layout is very different. One source unit /"cpp file"/ could handle as much declarations as you wish to put in to. However it is not necessary a good strategy since at some point you need to tell to other classes how to deal with your classes. In C++ a class can have a separated definition and declaration section and you are able to place them in to different files. The definition of the class is shared between other source files with #include statements so you can use those classes in any scenario where the definition is needed. If you're dealing with classes which are used only in one source file, you can hide it right in to the source files, but if you would like to connect them to other classes you need to put the definition in to a header file.
问题来自 java/C# 风格的源代码表示。在 C++ 中,源代码布局非常不同。一个源单元/“cpp 文件”/ 可以处理您希望放入的尽可能多的声明。然而,这并不是一个好的策略,因为在某些时候你需要告诉其他类如何处理你的类。在 C++ 中,一个类可以有一个单独的定义和声明部分,你可以将它们放在不同的文件中。使用#include 语句在其他源文件之间共享类的定义,因此您可以在需要定义的任何场景中使用这些类。如果您正在处理仅在一个源文件中使用的类,您可以将其隐藏在源文件中,
It could help you to improve the compilation speed - especially if you're using pre-compiled headers, but caresully, because pch-s are not part of the C++ standard and every compiler treats them in a different way! - and the flexibility of your code. Your teacher should be aware of these facts...
它可以帮助您提高编译速度 - 特别是如果您使用预编译的头文件,但要小心,因为 pch-s 不是 C++ 标准的一部分,每个编译器都以不同的方式对待它们!- 以及代码的灵活性。你的老师应该知道这些事实......