What does const after function header do?

This question already has an answer here:

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

  • From the Const Correctness tutorial:

    If you have a const object, you don't want to call methods that can change the object, so you need a way of letting the compiler know which methods can be safely called. These methods are called "const functions", and are the only functions that can be called on a const object. Note, by the way, that only member methods make sense as const methods. Remember that in C++, every method of an object receives an implicit this pointer to the object; const methods effectively receive a const this pointer.

    It might be worthwhile (spoiler: it is) to read through the whole article if you're new to the concept of constness.


    这意味着功能(操作员)不会更改对象。


    Effectively makes the "this" pointer a pointer to a const object. It means that members of the object cannot be modified in that method, nor can that method be invoked on a non-const object.

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

    上一篇: 一个函数的const声明

    下一篇: 函数头后的const是什么?