类成员函数声明疑问
我正在阅读 C++ 教程,并且遇到了这句话:
定义类成员函数的唯一区别 完全属于其类别或仅包含原型及更高版本 它的定义是,在第一种情况下,函数将 编译器自动将其视为内联成员函数, 而在第二个中它将是一个普通(非内联)类成员 函数,实际上假设行为没有差异。
我知道什么是内联函数,我的疑问是选择哪种样式。我应该在类内部还是外部定义每个函数?也许内部功能最简单,外部功能最简单?
我担心在类中定义每个函数(即具有复杂的内联函数)可能会弄乱生成的代码并在执行过程中引入调试问题或奇怪的行为。最后,还有“编码风格”问题。所以,
哪种方法更好?
谢谢:)
I'm reading a C++ tutorial and I've ran into this sentence:
The only difference between defining a class member function
completely within its class or to include only the prototype and later
its definition, is that in the first case the function will
automatically be considered an inline member function by the compiler,
while in the second it will be a normal (not-inline) class member
function, which in fact supposes no difference in behavior.
I know what an inline function is, my doubt is about which style to choose. Should I define every function inside its class or outside? Perhaps the simplest functions inside and the other outside?
I fear that defining every function inside a class (i.e. having complex inline functions) might mess up the resulting code and introduce debugging problems or weird behaviors during execution. And, finally, there's the "coding style" issue. So,
which approach is better?
Thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的风格:有时我会在类本身中放置非常短的(一两个衬垫)函数。我仍然希望作为内联函数的任何更长的内容都可以在类定义之后作为内联限定实现,并且通常在一个单独的文件中,标头位于末尾的#include类定义。
将内联函数放在类外部的基本原理是,某些函数的实现通常会妨碍读者对类的整体理解。二十行函数通常可以用一行注释来概括——当您阅读类定义时,该注释就是所需要的。如果您需要更多信息,请转到函数定义,或者更好的是,阅读精美文档。 (期望有人阅读该死的代码并不能替代优秀的文档。)
My style: I sometimes will put extremely short (one or two liner) functions in the class itself. Anything longer that I still want as an inlined function go as
inline
qualified implementations after the class definition, and oftentimes in a separate file that the header#include
s at the end of the class definition.The rationale for putting an inlined function outside the class is that the implementation of some function usually just gets in the way of a human reader's overall understanding of the class. A twenty line function can usually be summarized in a one line comment -- and that comment is all that is needed when you are reading the class definition. If you need more, go to the function definition, or better yet, Read The Fine Documentation. (Expecting someone to Read The F*** Code is a poor substitute for Fine Documentation.)
最好的解决方案是分离接口和实现。 Interface 是你的 h 文件。只把原型放在那里。实现转到 cpp 文件。这种方法具有以下优点:
The best solution is to separate interface and implementation. Interface is your h-file. Put only prototypes there. Implementation goes to cpp-file. This approach has the following advantages:
回答“哪种方法更好?”的部分。 - 来自 C++ 常见问题解答 -
To answer the part of "Which approach is better?" - From C++ FAQ -
这两种方法本身都没有更好,这只是偏好和风格的问题。就我个人而言,我始终认为在单独的 .inline 文件中显式定义函数是最好的方法。这样你就可以非常明确地了解你所做的事情并保持头文件的干净。
此外,如果您使用诸如 INLINE 之类的宏,其定义如下:
然后您可以在发布中包含来自标头的内联文件,并在调试中包含来自 CPP 的内联文件。这意味着即使编译器在调试中内联函数,您在调试时也不会有任何困难。不过,诚然,这对于如今的编译器来说并不是一个问题,因此您可能希望跳过这样做,除非使用旧的编译器。
Neither approach is better per se its a matter of preference and style. Personally I always think that defining the functions explicitly in a seperate .inline file is the best way. This way you are very explicit over what you do and you keep the header file clean.
Furthermore if you use a macro such as INLINE which is defined as follows:
You can then include the inline file from the header in release and from the CPP in debug. This means that even if the compiler inlines functions in debug you won't have any difficulties when debugging. Admittedly, though, this isn't such a problem for compilers these days so you may want to skip doing this unless using an old compiler.
一般来说,只有一两个语句的成员函数最好将其主体写在类声明中,尤其是在有很多语句的情况下。具有超过 20-50 条语句的成员函数可能最好不要出现在类声明中。对于长度和复杂性,它取决于许多因素。
例如,当类声明仅更改成员函数体时,将函数体放在类模块中有助于防止不必要的依赖模块重新编译。这可以大大提高开发类时的生产力。一旦班级稳定了,这个问题就变得不那么重要了。
Generally speaking, a member function which has only one or two statements is probably best with its body written in the class declaration—especially if there are many of them. A member function with more than 20-50 statements is probably best not in the class declaration. For lengths and complexities between, it depends on many factors.
For example, having the function body in a class module helps prevent unnecessary recompiling of dependent modules when the class declaration does not change—only a member function body. This can greatly increase productivity when developing the class. Once the class is stable, this becomes much less important.