This question already has an answer here:
- Store derived class objects in base class variables 4 answers
- What is object slicing? 17 answers
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