我在互联网上搜索了很多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)你能告诉我哪种方式是最好的还是最有效的实现方式?谢谢你的帮助:)
发布于 2016-12-05 07:14:53
1和2是相同的。它完全依赖于编译器实际以内联方式调用它。如果在类中定义了一个复杂的“内联”函数,那么写内联并不能保证它是内联的。因此,简而言之,它依赖于编译器。
https://stackoverflow.com/questions/40968863
复制相似问题