What's the difference between a const member function and a non-const member function?

后端 未结 5 1949
清歌不尽
清歌不尽 2020-11-29 11:17

I am very confused about the const version and non-const version member function like below:

value_type& top() { return this.item }
const value_type&         


        
5条回答
  •  一向
    一向 (楼主)
    2020-11-29 11:43

    When member function is declared as const what's happening is that the implicit this pointer parameter passed to the function is typed to be a pointer to a const object. This allows the function to be called using a const object instance.

    value_type& top();    // this function cannot be called using a `const` object
    const value_type& top() const; // this function can be called on a `const` object
    

提交回复
热议问题