If I have a C++ function/method, for example:
getSound(Animal a){
a.printSound();
}
and then pass it a Dog object that extends the class Animal but overrides Animal's printSound() method, is there a way of using Dog's printSound() within getSound()?
I have tried making printSound() in the Animal class definition virtual, but I am still getting the output of the original printSound().
Thanks in advance.
Making printSound virtual was correct. Change the signature of getSound to take a Animal& or const Animal&. By taking an Animal by value you are constructing a new Animal from your Dog, and that's just an Animal, not a Dog.
It is because of object-slicing, as you accept the argument by value.
Accept it by reference as:
void getSound(Animal & a); //now reference
If Animal::printSound() doesn't change the state of the object, then make it const member function (if it isn't const already), and then accept the argument by const reference as:
void getSound(Animal const & a); //now const reference
When you call getSound, you are passing the Animal by value. This means that a copy of the Dog is made by calling the Animal's copy constructor. The Animal's copy constructor constructs an Animal, not a Dog. You probably want pass by reference:
getSound(Animal& a){
a.printSound();
}
You've basically done everything right, except for one thing.
Either pass the Animal object by reference
getSound(Animal &a);
Or provide a pointer to the object in question.
getSound(Animal *a) {
a->printSound(); //Mind the -> in this case.
}
To call this function, you'd go about like this:
Dog D;
getSound(&D); //Passes a pointer to the function.
Otherwise, you'll construct a new object of type 'Animal', instead of really 'passing' a Dog.
Actually, you're best off with the pointer solution, otherwise you'll run into problems when passing a derived object, since it will expect an object of type Animal and will not settle for anything else.
If I have a C++ function/method, for example:
getSound(Animal a){
a.printSound();
}
and then pass it a Dog object that extends the class Animal but overrides Animal's printSound() method, is there a way of using Dog's printSound() within getSound()?
I have tried making printSound() in the Animal class definition virtual, but I am still getting the output of the original printSound().
Thanks in advance.
Making printSound virtual was correct. Change the signature of getSound to take a Animal& or const Animal&. By taking an Animal by value you are constructing a new Animal from your Dog, and that's just an Animal, not a Dog.
It is because of object-slicing, as you accept the argument by value.
Accept it by reference as:
void getSound(Animal & a); //now reference
If Animal::printSound() doesn't change the state of the object, then make it const member function (if it isn't const already), and then accept the argument by const reference as:
void getSound(Animal const & a); //now const reference
When you call getSound, you are passing the Animal by value. This means that a copy of the Dog is made by calling the Animal's copy constructor. The Animal's copy constructor constructs an Animal, not a Dog. You probably want pass by reference:
getSound(Animal& a){
a.printSound();
}
You've basically done everything right, except for one thing.
Either pass the Animal object by reference
getSound(Animal &a);
Or provide a pointer to the object in question.
getSound(Animal *a) {
a->printSound(); //Mind the -> in this case.
}
To call this function, you'd go about like this:
Dog D;
getSound(&D); //Passes a pointer to the function.
Otherwise, you'll construct a new object of type 'Animal', instead of really 'passing' a Dog.
Actually, you're best off with the pointer solution, otherwise you'll run into problems when passing a derived object, since it will expect an object of type Animal and will not settle for anything else.
0 commentaires:
Enregistrer un commentaire