C ++:是否是getter和setter内联的好习惯? [英] C++: Is it good practice to make getters and setters inline?

查看:322
本文介绍了C ++:是否是getter和setter内联的好习惯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public:
     inline int GetValue() const {
          return m_nValue;
     }
     inline void SetValue(int nNewValue) {
          this -> m_nValue = nNewValue;
     }

在Learn C ++上,他们说它会运行得更快。所以,我认为这将是巨大的。

但也许,有一些负面的点。

On Learn C++, they said it would run faster. So, I thought it would be great.
But maybe, there are some negative points to it.

推荐答案

p>我不内联任何东西,直到profiler具体告诉我,内联不会导致性能问题。

I don't inline anything until a profiler has specifically told me that not inlining is resulting in a performance problem.

C ++编译器非常聪明,几乎肯定会自动内联这样简单的功能。通常它比你聪明,并且会做更好的工作,以确定什么应该或不应该内联。

The C++ compiler is very smart and will almost certainly automatically inline such simple function like this for you. And typically it's smarter than you are and will do a much better job at determining what should or should not be inlined.

我会避免思考什么是或不要内联并专注于解决方案。稍后添加 inline 关键字(这不是内联BTW的保证)非常容易做,潜在的地方可以通过分析器轻松找到。

I would avoid thinking about what to or not to inline and focus on the solution. Adding the inline keyword later (which is not a guarantee of inline BTW) is very easy to do and potential places can be found readily with a profiler.

这篇关于C ++:是否是getter和setter内联的好习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆