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
castingAndTernary.cpp:15: error: conditional expression between distinct pointer types ‘D1*’ and ‘D2*’ lacks a cast
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;