编译器是否优化了这部分代码(const getter)?

时间:2011-12-11 20:17:48

标签: c++ oop compiler-construction

我有以下类定义:

class foo {
  private:
    bool m_active;

  public:
    const bool isActive() const // (btw do I need return type `const bool&` here?)
    {
       return m_active;
    }  
};
  1. 具有const getter(foo->isActive())的班级是否比foo->m_active更快(如果它是公开的)?我试着看看反汇编的代码,但没有找到任何有趣的东西。

  2. 我在哪里可以阅读有关const getter和setter的内容?我需要深入了解这些方法的使用地点和原因。

1 个答案:

答案 0 :(得分:6)

默认情况下,所有成员函数都被考虑用于函数内联。这意味着编译器将优化整个函数调用,并将其替换为对成员的直接访问。

所以答案是肯定的。编译器将对其进行优化。

相关问题