void const f() vs void f() const
阿新 • • 發佈:2018-12-14
void const f()
is equivilent to const void f()
, which means the return type (in this case a void) is const. This is totally meaningless not only because it's a void (there is nothing there that needs a const qualifier), but also because it's a return type (returning something as const doesn't make a whole lot of sense).void f() const
class A { public: void Const_No(); // nonconst member function void Const_Yes() const; // const member function }; //----------- A obj_nonconst; // nonconst object obj_nonconst.Const_No(); // works fine obj_nonconst.Const_Yes(); // works fine const A obj_const = A(); // const object obj_const.Const_Yes(); // works fine (const object can call const function) obj_const.Const_No(); // ERROR (const object cannot call nonconst function)