C++ 内联 - 什么是"正确"的方式
C++ Inlining - What is the "right" way
我在Internet上搜索了很多插入C 的内线,但似乎每个人都喜欢另一种实施方式。
我的问题如下:
// header-file
class Test {
int i;
public:
int getI();
};
// source-file
int Test::getI() { return i; }
由于此功能getI()
被调用数千次,我认为"直列"此功能很有用。最好的方法是什么:
// 1) define the function within the class-definition
class Test {
int i;
public:
int getI() { return i; }
};
// 2) define the function within the header-file
inline int Test::getI() { return i; } // directly located under class-definition
// 3) let the fct-definition stay in the source file and write "inline" before it (somehow this does not compile)
您能给我一个提示,哪种方法是最佳或大多数性能的实施方式?感谢您的帮助:)
1和2是相同的。完全取决于编译器,以内联的方式将其调用。如果您在课堂内定义一个复杂的"内联"功能,则写入内联该功能不能保证它是内联的。因此,简而言之,它取决于编译器。
相关文章:
- 用初始化列表和超类构造函数声明子类构造函数的正确方式
- 在地图上启动对象的正确方式
- 哈希多态类型的正确方式
- 设置内联函数的正确方式
- Qt中数据类(模型)和视图/控制器类之间的数据通信的正确方式是什么
- 复制QMimeData对象的正确方式
- 实现迭代器通用方法的正确方式
- 初始化容器中对象的正确方式/模式
- 从C++注册预关闭通知的正确方式
- 成员类之间的通信的正确方式是什么?
- 在Direct3D中指定索引的正确方式
- 使用QThread运行可管理后台线程的正确方式
- 从网络应用程序拆包操作类型的正确方式
- 定义类型的正确方式(typedef与#define)
- 使用其他库分发应用程序的"正确方式"
- 这是使用移动引用和unique_ptr的正确方式吗
- OOP中的正确方式.游戏示例.Player::walk或Map::playerWalk
- 访问用new声明的数据的正确方式
- 这是访问类的数据成员的正确方式吗
- CMake:拥有不同链接标志的正确方式