<small id='M9JSC'></small><noframes id='M9JSC'>

<legend id='M9JSC'><style id='M9JSC'><dir id='M9JSC'><q id='M9JSC'></q></dir></style></legend>
      <bdo id='M9JSC'></bdo><ul id='M9JSC'></ul>
      <i id='M9JSC'><tr id='M9JSC'><dt id='M9JSC'><q id='M9JSC'><span id='M9JSC'><b id='M9JSC'><form id='M9JSC'><ins id='M9JSC'></ins><ul id='M9JSC'></ul><sub id='M9JSC'></sub></form><legend id='M9JSC'></legend><bdo id='M9JSC'><pre id='M9JSC'><center id='M9JSC'></center></pre></bdo></b><th id='M9JSC'></th></span></q></dt></tr></i><div id='M9JSC'><tfoot id='M9JSC'></tfoot><dl id='M9JSC'><fieldset id='M9JSC'></fieldset></dl></div>
      1. <tfoot id='M9JSC'></tfoot>

        在头文件与实现 (.cpp) 文件中定义构造函数

        Defining constructor in header file vs. implementation (.cpp) file(在头文件与实现 (.cpp) 文件中定义构造函数)

          <bdo id='MuhDl'></bdo><ul id='MuhDl'></ul>

          <small id='MuhDl'></small><noframes id='MuhDl'>

          <tfoot id='MuhDl'></tfoot>
            <tbody id='MuhDl'></tbody>

          • <legend id='MuhDl'><style id='MuhDl'><dir id='MuhDl'><q id='MuhDl'></q></dir></style></legend>
              <i id='MuhDl'><tr id='MuhDl'><dt id='MuhDl'><q id='MuhDl'><span id='MuhDl'><b id='MuhDl'><form id='MuhDl'><ins id='MuhDl'></ins><ul id='MuhDl'></ul><sub id='MuhDl'></sub></form><legend id='MuhDl'></legend><bdo id='MuhDl'><pre id='MuhDl'><center id='MuhDl'></center></pre></bdo></b><th id='MuhDl'></th></span></q></dt></tr></i><div id='MuhDl'><tfoot id='MuhDl'></tfoot><dl id='MuhDl'><fieldset id='MuhDl'></fieldset></dl></div>
                  本文介绍了在头文件与实现 (.cpp) 文件中定义构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我可以在类 .h 文件或实现文件 .cpp 中定义类构造函数的主体.就特定项目中的编译器而言,这两种风格可能是相同的(项目对我来说意味着 DLL).这同样适用于任何成员函数:它们可以在头文件中定义,也可以在头文件中声明,然后在 cpp 文件中定义.

                  I can define the body of a class constructor in the class .h file or in the implementation file .cpp. These two styles are probably identical as far as the compiler is concerned within a specific project (project for me means DLL). Same applies to any member functions really: they can be defined in the header file or just declared there and then defined in the cpp file.

                  然而,我发现如果我需要在不同的项目中包含这样的类头文件(意味着最终使用头文件的代码最终在不同的 DLL 中)然后有头文件中的实际实现在编译时引起了一些麻烦(而不是链接......我什至没有达到那个点).为什么?好吧,我不会详细介绍,但编译器显然会尝试解析可能在其他头文件等中定义的所有函数,从而迫使可怜的开发人员开始拉入各种头文件等.

                  However, I found that if I need to include such class header file(s) in different projects (meaning that ultimately the code that uses the header file ends up in a different DLL) then having the actual implementation in the header file causes some headaches at compilation (not at linking... I don't even get to that point). Why? Well I won't go too much in detail but the compiler obviously tries to resolve all the functions which might be defined in other header files etc., forcing the poor developer to start pulling in various header files etc.

                  保持头文件不受任何实现的影响并仅将它们用于声明",这不是最好的做法吗?这将使将它们包含在多个项目中变得更加容易,而不必携带大量额外的垃圾.

                  Isn't it always best to keep header files free of any implementation and just use them for 'declarations'? That would make it easier to include them in more than one projects w/o having to carry around a lot of extra junk.

                  您对此有何看法?

                  推荐答案

                  需要注意的重要一点是,如果在头文件中定义了成员函数,则它必须要么在类体内,要么必须显式标记为内联.换句话说,在头文件中这样做是完全错误的:

                  One important point to note is that if a member function is defined inside a header file, it must be either inside the class body or it must be marked explicitly as inline. In other words, it is plain wrong to do this in a header file:

                  class A {
                    public:
                      A();
                  };
                  
                  A::A() {
                    // constructor body
                  }
                  

                  之所以出错,是因为它会使编译器在每个编译单元中都包含定义,而很明显,任何函数都必须只定义一次.以下是做同样事情的正确方法:

                  The reason it's wrong is because it will make the compiler include the definition in each compilation unit, while it's obvious that any function must be defined only once. Here are correct ways to do the same thing:

                  class A {
                    public:
                      inline A();
                  };
                  
                  inline A::A() {
                    // constructor body
                  }
                  

                  或者:

                  class A {
                    public:
                      inline A() { // inline isn't required here, but it's a good style
                       // constructor body
                      }
                  };
                  

                  在这两种情况下,构造函数都是内联的.使其成为常规外联函数的唯一正确方法是在实现文件中而不是在头文件中定义它.这是这两种方法之间最重要的区别.

                  In both cases the constructor is inline. The only correct way to make it a regular out-of-line function would be to define it in the implementation file, not in the header. This is the most important difference between these two approaches.

                  现在,值得注意的是内联是一种优化.与优化一样,在证明有必要之前最好避免使用它们.在内联可能导致的其他问题中,存在兼容性问题:如果您更改未内联的函数体,则只需重新编译定义它的单元,每个人都会立即开始使用新的实现.使用内联函数,您需要重新编译包含相关标头的每个单元,这可能会很痛苦,尤其是当不同的人在不同的项目中使用标头时.

                  Now, it is worth noting that inlining is an optimization. And as always with optimizations, they are best avoided until proved necessary. Among other problems inlining can lead to, there is the compatibility problem: if you change the body of a function that is not inlined, you only need to recompile the unit where it is defined, and everyone starts using the new implementation right away. With inlined functions, you need to recompile every unit that includes the relevant header, which can be a pain, especially if the header is used across different projects by different people.

                  换句话说,尽可能使用常规的外部定义,直到通过分析证明特定的函数调用是性能瓶颈.这条规则唯一合理的例外是微不足道的 setter 和 getter,即使使用它们也最好小心 - 有一天它们可能变得非常重要,这将意味着大量的重新编译和兼容性破坏.

                  In other words, use regular out-of-line definitions wherever possible until it is proved by profiling that a particular function call is a performance bottleneck. The only reasonable exception to this rule are trivial setters and getters, and even with them it is better to be careful - one day they may become non-trivial and it will mean a lot of recompilation and compatibility breaking.

                  这篇关于在头文件与实现 (.cpp) 文件中定义构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                1. <i id='ACNgl'><tr id='ACNgl'><dt id='ACNgl'><q id='ACNgl'><span id='ACNgl'><b id='ACNgl'><form id='ACNgl'><ins id='ACNgl'></ins><ul id='ACNgl'></ul><sub id='ACNgl'></sub></form><legend id='ACNgl'></legend><bdo id='ACNgl'><pre id='ACNgl'><center id='ACNgl'></center></pre></bdo></b><th id='ACNgl'></th></span></q></dt></tr></i><div id='ACNgl'><tfoot id='ACNgl'></tfoot><dl id='ACNgl'><fieldset id='ACNgl'></fieldset></dl></div>

                  <small id='ACNgl'></small><noframes id='ACNgl'>

                      • <bdo id='ACNgl'></bdo><ul id='ACNgl'></ul>

                        <tfoot id='ACNgl'></tfoot>

                              <tbody id='ACNgl'></tbody>
                          • <legend id='ACNgl'><style id='ACNgl'><dir id='ACNgl'><q id='ACNgl'></q></dir></style></legend>