简体   繁体   English

C ++类构造函数

[英]C++ class constructor

I have a skeleton for a class as follow: 我有一个类的骨架,如下所示:

class MyList
{
    private:
    public:MyList(int);    // why can't I define stuffs here?
};

MyList::MyList(int s)     // why is this outside of the class?
{
}

I wonder why the constructor is put outside of the class itself? 我想知道为什么构造函数放在类本身之外? Wouldn't it be more compact and nicer to have it placed inside the class itself? 将其放置在类本身中会不会更紧凑,更美观?

You absolutely can make the constructor inline ("Java style") if you want, and for simple one-lined methods that are unlikely to change very often, that is often the way to go. 如果需要,您绝对可以使构造函数内联(“ Java样式”),对于不太可能经常更改的简单的单行方法,这通常是可行的方法。

The question you may be asking is, what is the point of allowing the non-inline version, when doing it inline requires less typing and fewer files to manage? 您可能会问的问题是,允许非内联版本进行内联时有什么意义?

There are several reasons you might want to do a non-inline constructor (or non-inline style of any method, really): 您可能有几个原因想要做一个非内联构造函数(或者实际上是任何方法的非内联样式):

  1. Since .h files get #included by other files, anytime you change a .h file, any .cpp file that #includes it (as well as any .cpp files that include a .h file that includes your .h file) may need to be recompiled. 由于.h文件被其他文件#included包含,因此,每次更改.h文件时,可能需要任何包含#h的.cpp文件(以及包含.h文件的.cpp文件和包含.h文件的任何.cpp文件)重新编译。 This can make development slow if your program is large and/or you need to change inline methods a lot. 如果您的程序很大和/或您需要大量更改内联方法,这可能会使开发变慢。

  2. Separating the method's body from its definition gives you a way to solve the circular-include problem -- eg it would allow you to have two classes whose method arguments each refer to the other class (by pointer or reference). 将方法的主体与其定义分开,可以为您解决循环包含问题的方法 -例如,它将允许您拥有两个类,其方法参数分别引用另一个类(通过指针或引用)。 With only inline code, the method-bodies of one of those two classes would have to be parsed before the other class's .h file was parsed, which would likely result in "incomplete type" or "unknown type" compilation errors. 仅使用内联代码,就必须在解析另一个类的.h文件之前解析这两个类之一的方法主体,这很可能会导致“不完整类型”或“未知类型”的编译错误。

I wonder why the constructor is put outside of the class itself? 我想知道为什么构造函数放在类本身之外?

The function definition can be put outside or it can be kept inside. 函数定义可以放在外面,也可以放在里面。 This applies to all member functions, not just constructors. 这适用于所有成员函数,而不仅仅是构造函数。 So, let me make your question more general: What is the advantage of defining member functions outside of the class? 因此,让我让您的问题更笼统:在类外部定义成员函数有什么好处?

It allows keeping the definition of the class short and concise. 它可以使类的定义简短明了。 This is useful in keeping the class definition readable: The member declarations are the most useful information to the user of a class who reads its definition. 这对于保持类定义的可读性很有用:成员声明对于读取其定义的类的用户而言是最有用的信息。 The definition of the member functions is often an implementation detail that can't or shouldn't be relied on. 成员函数的定义通常是一个不能或不应该依赖的实现细节。

It also allows defining the member functions in a separate source file instead of being defined inline in all of the source files that include the class. 它还允许在单独的源文件中定义成员函数,而不是在内联的所有源文件中内联定义。

So, the advantages are same as separating forward declarations of free functions from their definition. 因此,优点与将自由函数的前向声明与它们的定义分开一样。 A list of function declarations in a header is analogous to a concise class definition with no inline function definitions. 标头中的函数声明列表类似于简洁的类定义,没有内联函数定义。

Defining a function (member or otherwise) out-of-line in its own source file instead of inline of all source files is useful because it allows that single source file to be compiled separately. 在其自己的源文件中而不是在所有源文件的内联中离线定义函数(成员或其他)是很有用的,因为它允许单独编译单个源文件。 If any change is made to the function, no other source file need to be compiled. 如果对该函数进行了任何更改,则无需编译其他源文件。 In the inline case, all source files that use the function would have to be compiled again. 在嵌入式情况下,所有使用该功能的源文件都必须再次编译。 Likewise, whenever any other source file is compiled, there is no need to compile the out-of-line function from another source file, but an inline function must be compiled in each source file. 同样,无论何时编译任何其他源文件,都无需从另一个源文件编译脱机函数,但是必须在每个源文件中编译一个内联函数。


These general advantages hardly apply to your trivial example constructor. 这些一般优点几乎不适用于您的琐碎示例构造函数。 It is an ideal candidate for inline definition, because its call can be completely optimized away. 它是内联定义的理想选择,因为可以完全优化其调用。 Also, it is so short that it doesn't affect readability of the class. 而且,它太短了以至于不影响类的可读性。 As such, I would recommend to not define that constructor outside of the class itself. 因此,我建议不要在类本身之外定义该构造函数。

You can place the constructor definition in the class definition. 您可以将构造函数定义放在类定义中。 Both ways work just like other functions. 两种方式都像其他功能一样工作。 Where you place it depends on stuff like the length of the definition. 放置位置取决于诸如定义长度之类的东西。

You can put the constructor at either place. 您可以将构造函数放在任一位置。 The compiler does not make distinction. 编译器没有区别。 However, it is more of convention you follow in your team. 但是,在团队中遵循的惯例更多。 Some groups follow the convention on defining everything in the *.cpp file, starting with standard methods like constructor, destructor, copy-constructor, operator-overloading followed by user defined methods. 一些组遵循在* .cpp文件中定义所有内容的约定,从标准方法(例如构造函数,析构函数,复制构造函数,运算符重载,用户定义的方法)开始。

It makes the code easy to read, maintain as the codebase expands. 它使代码易于阅读,并随着代码库的扩展而得以维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM