Skip to content

Instantly share code, notes, and snippets.

@hongyangqin
Last active December 9, 2017 10:58
Show Gist options
  • Save hongyangqin/a7638016a78610f318d00d9a421ad6c9 to your computer and use it in GitHub Desktop.
Save hongyangqin/a7638016a78610f318d00d9a421ad6c9 to your computer and use it in GitHub Desktop.
inline函数必须在头文件中定义吗?

inline函数必须在头文件中定义吗?

From : http://blog.csdn.net/tonywearme/article/details/7097910

前不久在写代码的时候遇到一个link错误,代码的原型如下所示,基本就是定义了一个基类和派生类,在派生类的一个成员函数中用到了基类定义的一个内联函数。

// base.h
class Base
{
protected:
   void fun();
};

// base.cpp
#include "base.h"
inline void Base::fun()
{}

// derived.h
#include "base.h"
class Derived: public Base
{
public:
   void g();
};

// derived.cpp
void Derived::g()
{
   fun(); //VC2008: error LNK2019: unresolved external symbol
}

写这个内联函数的时候也没细想,结果违反了inline函数的要求。所谓内联函数,就是编译器将函数定义({...}之间的内容)在函数调用处展开,藉此来免去函数调用的开销。如果这个函数定义在头文件中,所有include该头文件的编译单元都可以正确找到函数定义。然而,如果内联函数fun()定义在某个编译单元A中,那么其他编译单元中调用fun()的地方将无法解析该符号,因为在编译单元A生成目标文件A.obj后,内联函数fun()已经被替换掉,A.obj中不再有fun这个符号,链接器自然无法解析。

所以,如果一个inline会在多个源文件中被用到,那么必须把它定义在头文件中。 在C++中,这意味着如果inline函数具有public或者protected访问属性,你就应该这么做。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment