What is the meaning of const in this function signature?

c++, constants

Solution

The second const means the method can be called on a const object.

Consider this example:

class foo
{
public:
    void const_method() const;
    void nonconst_method();
};

void doit()
{
    const foo f;

    f.const_method();      // this is okay
    f.nonconst_method();   // the compiler will not allow this
}

Furthermore, a const method is not allowed to change any members of the object (unless the member is specifically marked at mutable):

class foo
{
public:
    void const_method() const;

private:
    int r;
    mutable int m;
};

void foo::const_method() const
{
    m = 0; // this is okay as m is marked mutable
    r = 0; // the compiler will not allow this
}

Problem

Please help me understand the following signature: ``` err_type funcName(const Type& buffer) const; ``` For the first `const`, does that mean the contents of `Type` cannot change or that the reference cannot change? Secondly, what does the second `const` mean?

Original source