将getter和setter设置为内联代码是一种好习惯吗? [英] Is it good practice to make getters and setters inline?

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

问题描述

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

Learn C ++ 上,他们说它将运行快点。因此,我认为在getter和setter上使用它会很棒。但是,也许有一些缺点吗?

On Learn C++, they said it would run faster. So, I thought it would be great to use on getters and setters. But maybe, there are some drawbacks to it?

推荐答案

在分析器明确告诉我之前,我什么都不要内联内联会导致性能问题。

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 关键字(不保证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.

这篇关于将getter和setter设置为内联代码是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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