我在互联网上搜索了很多 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)
你能给我一个提示,哪种方式是最好的或最高效的实现方式?感谢帮助 :)