const CFoo&bar()const

dom*_*lao 7 c++

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

Mar*_*off 13

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

另请参阅C++ FAQ Lite条目" Fred const& X"是什么意思?什么是" const成员函数"?.


aJ.*_*aJ. 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
Run Code Online (Sandbox Code Playgroud)