代码之家  ›  专栏  ›  技术社区  ›  domlao

const cfoo&bar()常量

c++
  •  7
  • domlao  · 技术社区  · 16 年前

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

    3 回复  |  直到 15 年前
        1
  •  13
  •   Mark Rushakoff    16 年前

    方法 bar 返回对常量的引用 CFoo (那就是 const CFoo & 前部分 酒吧 ,并且调用此方法不会修改任何未标记为 mutable (那就是 const 在括号后面)。

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

        2
  •  9
  •   aJ.    16 年前
    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
    
        3
  •  0
  •   Jerry Coffin    16 年前

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