内衬方法:缺点

Inlining methods : drawbacks

本文关键字:缺点 方法      更新时间:2023-10-16

我有一个关于内联方法的问题。我正在使用一个为碰撞模型开发的库。负责图形接口的一个头文件包含函数的声明和实现,但函数不是内联的。因此,不可能将这些功能包括在几个翻译单元中。作为一个例子,这里有一个我为说明而设计的伪代码:

LivingBeing.h

#ifndef LIVINGBEING_H
#define LIVINGBEING_H
class LivingBeing
{
public:
    LivingBeing(double _size);
    void breathe();
private:
    double size;
};
//////////////
LivingBeing::LivingBeing(double _size)
{
    size = _size;
}
void LivingBeing::breathe()
{
    // do something
}
#endif

Forest.h

#ifndef FOREST_H
#define FOREST_H
#include "LivingBeing.h"
class Forest
{
public:
    Forest(int _numberLivingBeings);
private:
    int numberLivingBeings;
};
#endif

Forest.cpp

#include "Forest.h"
Forest::Forest(int _numberLivingBeings)
{
    numberLivingBeings = _numberLivingBeings;
            // Call LivingBeing constructor, methods etc...
}

主要.cpp

#include "Forest.h"
int main()
{
    Forest forest = Forest(10);
    return 0;
}

除非在构造函数LivingBeing和方法breath前面添加inline关键字,否则此代码不会编译。错误消息为:

1>main_test.obj : error LNK2005: "public: __thiscall LivingBeing::LivingBeing(double)" (??0LivingBeing@@QAE@N@Z) already defined in Forest.obj
1>main_test.obj : error LNK2005: "public: void __thiscall LivingBeing::breathe(void)" (?breathe@LivingBeing@@QAEXXZ) already defined in Forest.obj
1>C:Users******DocumentsVisual Studio 2010ProjectsTutorialChronoEngineDebugTest_3.exe : fatal error LNK1169: one or more multiply defined symbols found

我的问题是:内联方法的缺点是什么?我使用的真正的库非常大,我想从一个特定的文件(在我的例子中是LivingBeing.h)内联方法,这样就可以在几个.cpp文件中使用这些方法。这样更改源文件有什么风险?

非常感谢

您在标头中定义函数(LivingBeing::LivingBeingLivingBeing::breathe),这意味着每个翻译单元中都会有一个包含该标头的定义。这违反了一个定义规则(ODR),因此出现了链接错误。

你有三个选择:

  • 将函数定义移动到源文件中,使它们只定义一次;或
  • 将它们声明为inline,以允许多个相同的定义;或
  • 将定义移动到类定义中,这将隐式地使它们成为inline

内联方法的缺点是什么?

  • 可能增加了编译时间和可执行文件的大小;但你需要测量一下,看看是否有任何明显的差异
  • 一个不太稳定的API客户端代码需要在任何内联函数改变时重新编译
  • 意外破坏ODR的可能性,例如,如果函数包含宏,这些宏可能在不同的翻译单元中具有不同的扩展

仅仅因为方法是在"源"文件(.c/.cpp)中定义的,并不意味着它不会内联。。。链路时间操作可以执行该优化。相反,仅仅因为一个方法在头文件中被声明和实现为内联,并不意味着它将被内联。通常,如果方法非常简单,我会在头文件中定义方法,例如int getWidth() const { return width; }MyObj* getNext() { return internal_itor ? internal_itor->next : 0; }