内衬方法:缺点
Inlining methods : drawbacks
我有一个关于内联方法的问题。我正在使用一个为碰撞模型开发的库。负责图形接口的一个头文件包含函数的声明和实现,但函数不是内联的。因此,不可能将这些功能包括在几个翻译单元中。作为一个例子,这里有一个我为说明而设计的伪代码:
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::LivingBeing
和LivingBeing::breathe
),这意味着每个翻译单元中都会有一个包含该标头的定义。这违反了一个定义规则(ODR),因此出现了链接错误。
你有三个选择:
- 将函数定义移动到源文件中,使它们只定义一次;或
- 将它们声明为
inline
,以允许多个相同的定义;或 - 将定义移动到类定义中,这将隐式地使它们成为
inline
内联方法的缺点是什么?
- 可能增加了编译时间和可执行文件的大小;但你需要测量一下,看看是否有任何明显的差异
- 一个不太稳定的API客户端代码需要在任何内联函数改变时重新编译
- 意外破坏ODR的可能性,例如,如果函数包含宏,这些宏可能在不同的翻译单元中具有不同的扩展
仅仅因为方法是在"源"文件(.c/.cpp)中定义的,并不意味着它不会内联。。。链路时间操作可以执行该优化。相反,仅仅因为一个方法在头文件中被声明和实现为内联,并不意味着它将被内联。通常,如果方法非常简单,我会在头文件中定义方法,例如int getWidth() const { return width; }
或MyObj* getNext() { return internal_itor ? internal_itor->next : 0; }
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 在工厂中使用静态方法:优点和缺点?
- 使用 unique_ptr 作为方法参数 - 优点和缺点
- OOP——使用静态方法有缺点吗
- 在名称空间中组织变量和方法的优点和缺点
- cuda中虚拟方法的缺点
- 如何访问类变量?公共方法/getters与继承.优点和缺点是什么
- 使用类的方法而不实例化的优点/缺点
- 内衬方法:缺点