Casting pointers and the ternary ?: operator. Have I reinvented the wheel?

前端 未结 4 1597
无人共我
无人共我 2021-02-14 04:19

The last line of this code fails to compile with castingAndTernary.cpp:15: error: conditional expression between distinct pointer types ‘D1*’ and ‘D2*’ lacks a cast

4条回答
  •  旧时难觅i
    2021-02-14 05:06

    A really smart compiler could have no difficulty because both can be safely casted to B*

    Irrelevant. The standard mandates this behaviour. A really smart compiler behaves as observed.

    The use of your custom cast is actually fine (and your reluctance for using an explicit cast is well-placed). However, I’d use a different name: upcast – since that’s happening here: a cast upwards in the inheritance hierarchy.

提交回复
热议问题