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

前端 未结 4 1586
无人共我
无人共我 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条回答
  •  别那么骄傲
    2021-02-14 04:57

    I just came across this problem, lose the casts and do it the long way is cleanest IMO

    B * d1 = new D1();
    B * d2 = new D2();
    B * b = boolean_expression ? d1 : d2;
    

提交回复
热议问题