0

Dog is base object

class dog {
    void bark(); (print : I am dog)
}

and a lot of child class from dog

class Puppy : Dog {
    void bark(); (print : I am a purry)
}

class Puddle : Dog {
    void bark(); (print : I am a puddle)
}

put many kinds of child class into "Vector dogs"

Vector<Dog *> dogs;

dogs.pushBack(puppy);
dogs.pushBack(puddle);
dogs.pushBack(martiz);
dogs.pushBack(yok);

for (auto dog : dogs) {
    dog->bark();
}

all class print "I am a dog"

I want to downcast automatically their origin class.

what is the best way for my case?

o242
  • 51
  • 1
  • 7

1 Answers1

0
class dog {
   virtual void bark(); (print : I am dog)
}

you should set the bark to a virtual void bark and it should work

Druide
  • 48
  • 1
  • 6