mercredi 1 août 2018

Possible to assign subclass instance to baseclass declaration? [duplicate]

This question already has an answer here:

In C++ is it possible to do the following?

// class A
//    explicit A(){};
// class B : public A
//    using A::A;

int main()
{
    A a = B();
}

Obviously a generic case, a more specific use case would be:

int main()
{
    Car c;
    int choice;
    bool picked = false;
    do
    {
        cin >> choice;
        if (choice == 1)
        {
            c = Honda();
            picked = true;
        else
        {
            c = Ford();
            picked = true;
        }
    } while (!picked);
}

Aucun commentaire:

Enregistrer un commentaire