Calling a const function rather than its non-const version

前端 未结 4 1439
深忆病人
深忆病人 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:59

    test is a non-const object, so the compiler finds the best match: The non-const version. You can apply constness with static_cast though: static_cast(test)->x();

    EDIT: As an aside, as you suspected 99.9% of the time you think you've found a compiler bug you should revisit your code as there's probably some weird quirk and the compiler is in fact following the standard.

提交回复
热议问题