operands to ?: have different types, downcast du subclass

Viewed 48

Code with issue:

class A {
    public:
    int i = 2;
};

class B : public A {};
class C : public A {};

int main()
{
    A a;
    B b;
    C c;
    
    auto x = a.i==2 ? c:b;
}

What confuses me, is that when I replace either c orb with a, it seems to recognize that they share a class and will downcast appropriately. So what do I have to do to make this happen here too? I want to avoid an explicit cast to not clutter up the code, but is there any other way?

0 Answers
Related