内联函数以及类和头文件 [英] inline function and class and header file
本文介绍了内联函数以及类和头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 头文件中定义的任何函数会自动内联吗?
- 如果我在类中声明一个函数并使用关键字inline给出外部定义,那么该函数会是inline吗?如果是这样,为什么这样做不违反法律,即在声明时将内联函数赋予主体?
推荐答案
在类定义内定义的任何函数都是内联的.标记为inline
的任何函数也是内联的.
Any function defined inside a class definition is inline. Any function marked inline
is also inline.
class C {
int f() { return 3; } // inline
int g();
int h();
}
inline int C::g() { return 4; } // inline
int C::h() { return 5; } // not inline
如果所有这些代码都在一个标头中,并且该标头用于多个翻译单元,您会抱怨C::h
具有多个定义. C::f
和C::g
可以,因为它们是内联的.这就是inline
如今的主要作用:允许在多个位置定义相同的功能(前提是定义是相同的").
If all this code is in a header and that header is used in more than one translation unit you'll get a complaint that C::h
has more than one definition. C::f
and C::g
are okay because they're inline. That's the primary role of inline
these days: to permit defining the same function in multiple places (provided the definitions are "the same").
这篇关于内联函数以及类和头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文