Why can I call a non-const member function pointer from a const method?

前端 未结 5 781
终归单人心
终归单人心 2020-12-11 05:42

A co-worker asked about some code like this that originally had templates in it.

I have removed the templates, but the core question remains: why does this compile O

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-11 06:38

    You are calling foo on object, not on this.

    Since object is declared as an X&, in a constant CX, it is actually an X& const (which is not the same as const X&) allowing you to call non const methods on it.

提交回复
热议问题