问题描述
我记得在一本C ++的书中(很久以前)读到,内联的构造函数和析构函数,特别是对于派生类是不是一个好主意。
我知道内联会导致一些膨胀的目标代码,但有任何其他设计考虑,阻止内联构造函数和析构函数?当然大多数编译器可以拒绝内联并继续创建一个函数体,但是如果他们内联了什么惩罚可能需要支付?
I remember reading in one of the C++ books (quite some time ago) that it is not a good idea to have inline Constructors and Destructors especially for derived class. I understand that inlining would induce some bloating up of object code but are there any other design considerations that discourage inline constructors and destructors? Of course most compilers may reject the inline and proceed with creating a function body but if they were to inline what penalty might one have to pay?
推荐答案
编译器可以自由的内联代码,你没有声明内联,并且是免费的内联代码,你已经声明内联。我看到编译器做这两件事情。因为这个inline关键字并不意味着大多数人认为它。它的意思是允许一个定义规则的异常,所以你可以把函数等在头文件中,而不会得到链接器错误。
The compiler is free to inline code that you have not declared inline, and is free not to inline code that you have declared inline. I have seen compilers do both of these things. Because of this the inline keyword does not mean what most people think it does. It's meaning is to allow an exception to the one definition rule, so you can put functions etc. in a header file and not get linker errors.
所以建议是垃圾,让编译器决定什么是最好的内联和什么不是。在你需要它的地方,防止链接器错误,这是所有。
So the advice is rubbish, let the compiler decide what is best to inline and what is not. Put inline where you need it to prevent linker errors, that is all.
这篇关于为什么内联构造函数和析构函数在C ++中不是一个好主意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!