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

来源:互联网 发布:iptv管理系统php 编辑:程序博客网 时间:2024/06/10 07:51

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

[cpp] view plaincopyprint?

在CODE上查看代码片派生到我的代码片


  1. // base.h  
  2. class Base  
  3. {  
  4. protected:  
  5.    void fun();  
  6. };  
  7.   
  8. // base.cpp  
  9. #include “base.h”  
  10. inline void Base::fun()  
  11. {}  
  12.   
  13. // derived.h  
  14. #include “base.h”  
  15. class Derived: public Base  
  16. {  
  17. public:  
  18.    void g();  
  19. };  
  20.   
  21. // derived.cpp  
  22. void Derived::g()  
  23. {  
  24.    fun(); //VC2008: error LNK2019: unresolved external symbol  
  25. }  
[cpp] view plain copy
print?
  1. // base.h  
  2. class Base  
  3. {  
  4. protected:  
  5.    void fun();  
  6. };  
  7.   
  8. // base.cpp  
  9. #include “base.h”  
  10. inline void Base::fun()  
  11. {}  
  12.   
  13. // derived.h  
  14. #include “base.h”  
  15. class Derived: public Base  
  16. {  
  17. public:  
  18.    void g();  
  19. };  
  20.   
  21. // derived.cpp  
  22. void Derived::g()  
  23. {  
  24.    fun(); //VC2008: error LNK2019: unresolved external symbol  
  25. }  
// base.hclass 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.cppvoid 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访问属性,你就应该这么做。

阅读全文
0 0
原创粉丝点击