const CFoo& bar()const

时间:2010-04-30 03:10:48

标签: c++

我有一个类的属性,例如const CFoo &bar() const,它是什么意思?

3 个答案:

答案 0 :(得分:13)

方法bar返回对const CFoo的引用(即const CFoo &之前的bar部分),并且调用此方法不会修改任何不是的变量标记为mutable(括号后面的const)。

另请参阅C ++ FAQ Lite条目What does "Fred const& X" mean?What is a "const member function"?

答案 1 :(得分:9)

const CFoo& bar() const
----------      --------
      ^               ^
Returns a connst      None of the member variables of the class to which bar
reference of CFoo.    method belongs to can be modified.
                      unless member variable is prefexex with keyword mutable

答案 2 :(得分:0)

它是一个const函数(不会修改对象的任何非可变成员)成员函数,它返回对const CFoo的引用。