What does const T& value() const mean?

This question already has an answer here:

  • Meaning of “const” last in a C++ method declaration? 7 answers

  • const T& value() const {...}
    

    This means the function value() will return a const T& type and in between (in the function) won't modify the class itself. Say I write:

    class Cfoo
    {
        void foo() const
        {
            //Cfoo will not be modified here
        }
    }
    

    If I directly quote from msdn:

    Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. A constant member function cannot modify any non-static data members or call any member functions that aren't constant.


    The const after the method name signifies that the method does not modify any field of the object. Therefore, it can be invoked on a const instance of the object.

    链接地址: http://www.djcxy.com/p/40428.html

    上一篇: 在C ++中重载运算符时,“const”是什么意思?

    下一篇: const T&value()const是什么意思?