在C ++中,构造函数和析构函数可以是内联函数吗? [英] In C++ can constructor and destructor be inline functions?
本文介绍了在C ++中,构造函数和析构函数可以是内联函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我声明一个类 Foo
如下,那么是CONSTRUCTOR和DESTRUCTOR内联函数?
class Foo
{
int * p ;
public:
Foo(){p = new char [0x00100000]; }
〜Foo(){delete [] p; }
};
{
Foo f;
(f);
}
解决方案
定义构造函数INSIDE在类中使用inline关键字放置函数OUTSIDE的效果相同。
在这两种情况下,它都是编译器的提示。 内联函数不一定意味着函数将被内联。这取决于函数的复杂性和其他规则。
VC++ makes functions which are implemented within the class declaration inline functions.
If I declare a class Foo
as follows, then are the CONSTRUCTOR and DESTRUCTOR inline functions?
class Foo
{
int* p;
public:
Foo() { p = new char[0x00100000]; }
~Foo() { delete [] p; }
};
{
Foo f;
(f);
}
解决方案
Defining the body of the constructor INSIDE the class has the same effect of placing the function OUTSIDE the class with the "inline" keyword.
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.
这篇关于在C ++中,构造函数和析构函数可以是内联函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文