Using C++, how to call a base class method from a derived class method and apply this to an object passed as an argument?
Posted
by
Chris
on Stack Overflow
See other posts from Stack Overflow
or by Chris
Published on 2012-03-22T18:49:56Z
Indexed on
2012/03/24
17:29 UTC
Read the original article
Hit count: 280
I can't figure out how to call a base class method from a derived class method but concurrently applying this method call at an object passed as argument.
What I mean is this:
class Animal
{
virtual void eat(Animal& to_be_eaten) = 0;
};
class Carnivores: public Animal
{
virtual void eat(Animal& to_be_eaten) { /*implementation here*/}
};
class Wolf : public Carnivores
{
virtual void eat(Animal& to_be_eaten)
{ /*call eat method(of Base class) of Base to_be_eaten here*/ }
}
I thought of something like this
dynamic_cast<Carnivores&>(to_be_eaten).eat(*this) //and got a segmentation fault
Is there any way for this to be done?
Thank you!
New edit:: Updated the code
© Stack Overflow or respective owner