首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++内联-正确的方式是什么?

C++内联-正确的方式是什么?
EN

Stack Overflow用户
提问于 2016-12-05 07:09:23
回答 1查看 65关注 0票数 0

我在互联网上搜索了很多C++的内联内容,但似乎每个人都喜欢不同的实现方式。

我的问题如下:

代码语言:javascript
复制
// header-file
class Test {
    int i;
    public:
        int getI();
};
// source-file
int Test::getI() { return i; }

由于这个函数getI()被调用了几千次,我认为“内联”这个函数是有用的。这样做的最佳方式是什么:

代码语言:javascript
复制
// 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)

你能告诉我哪种方式是最好的还是最有效的实现方式?谢谢你的帮助:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-05 07:14:53

1和2是相同的。它完全依赖于编译器实际以内联方式调用它。如果在类中定义了一个复杂的“内联”函数,那么写内联并不能保证它是内联的。因此,简而言之,它依赖于编译器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40968863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档