Call a non-const member function from a const member function

后端 未结 7 1715
情深已故
情深已故 2020-12-29 06:15

I would like to know if its possible to call a non-const member function from a const member function. In the example below First gives a compiler error. I understand why it

7条回答
  •  抹茶落季
    2020-12-29 07:05

    I found myself trying to call a non-const member function that was inherited, but was actually const because of the API I was using. Finally I settled on a different solution: re-negotiate the API so that the function I inherit is properly const.

    It won't always be possible to negotiate changes to others' functions, but doing so when possible seems cleaner and nicer than needing to use const_cast and it benefits other users as well.

提交回复
热议问题