在 C++ 中,构造函数和析构函数可以是内联函数吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21303/
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
In C++ can constructor and destructor be inline functions?
提问by popopome
VC++ makes functions which are implemented within the class declaration inline functions.
VC++ 生成在类声明内联函数中实现的函数。
If I declare a class Foo
as follows, then are the CONSTRUCTOR and DESTRUCTOR inline functions?
如果我Foo
如下声明一个类,那么 CONSTRUCTOR 和 DESTRUCTOR 是内联函数吗?
class Foo
{
int* p;
public:
Foo() { p = new char[0x00100000]; }
~Foo() { delete [] p; }
};
{
Foo f;
(f);
}
采纳答案by popopome
Defining the body of the constructor INSIDE the class has the same effect of placing the function OUTSIDE the class with the "inline" keyword.
在类内部定义构造函数的主体与使用“inline”关键字将函数放在类外部具有相同的效果。
In both cases it's a hint to the compiler. An "inline" function doesn't necessarily mean the function will be inlined. That depends on the complexity of the function and other rules.
在这两种情况下,它都是对编译器的提示。“内联”函数并不一定意味着该函数将被内联。这取决于函数和其他规则的复杂性。
回答by Wilka
The short answer is yes. Any function can be declared inline, and putting the function body in the class definition is one way of doing that. You could also have done:
简短的回答是肯定的。任何函数都可以声明为内联,将函数体放在类定义中是一种方法。你也可以这样做:
class Foo
{
int* p;
public:
Foo();
~Foo();
};
inline Foo::Foo()
{
p = new char[0x00100000];
}
inline Foo::~Foo()
{
delete [] p;
}
However, it's up to the compiler if it actually does inline the function. VC++ pretty much ignores your requests for inlining. It will only inline a function if it thinks it's a good idea. Recent versions of the compiler will also inline things that are in seperate .obj files and not declared inline (e.g. from code in different .cpp files) if you use link time code generation.
但是,它是否确实内联了函数,这取决于编译器。VC++ 几乎忽略了您的内联请求。如果它认为这是一个好主意,它只会内联一个函数。如果您使用链接时间代码生成,最新版本的编译器还将内联位于单独 .obj 文件中且未声明为内联的内容(例如,来自不同 .cpp 文件中的代码)。
You could use the __forceinlinekeyword to tell the compiler that you really really mean it when you say "inline this function", but it's usally not worth it. In many cases, the compiler really does know best.
您可以使用__forceinline关键字告诉编译器,当您说“内联此函数”时,您确实是认真的,但这通常是不值得的。在许多情况下,编译器确实最了解。
回答by Mickey
Putting the function definition in the class body equivelent to marking a function with the inline keyword. That means the function may or may not be inlined by the compiler. So I guess the best answer would be "maybe"?
将函数定义放在类体中等同于使用 inline 关键字标记函数。这意味着该函数可能会或可能不会被编译器内联。所以我想最好的答案是“也许”?
回答by DrPizza
To the same extent that we can make any other function inline, yes.
与我们可以使任何其他函数内联的程度相同,是的。
回答by Rodney Schuler
To inline or not is mostly decided by your compiler. Inline in the code only hints to the compiler.
One rule that you can count on is that virtual functions will never be inlined. If your base class has virtual constructor/destructor yours will probably never be inlined.
内联与否主要由您的编译器决定。代码中的内联仅提示编译器。
您可以信赖的一条规则是虚函数永远不会被内联。如果您的基类具有虚拟构造函数/析构函数,则您的基类可能永远不会被内联。