Calling a const function rather than its non-const version

前端 未结 4 1445
深忆病人
深忆病人 2020-11-28 13:38

I tried to wrap something similar to Qt\'s shared data pointers for my purposes, and upon testing I found out that when the const function should be called, its non-const ve

4条回答
  •  半阙折子戏
    2020-11-28 13:48

    It doesn't matter whether Data::x is a constant function or not. The operator being called belongs to container class and not Data class, and its instance is not constant, so non-constant operator is called. If there was only constant operator available or the instance of the class was constant itself, then constant operator would have been called.

提交回复
热议问题