Let's say I have an empty class with a virtual function:
class Base
{
public:
virtual void Foo(){std::cout << "this is the base class";}
}
Then I have a class that inherits from Base
and overrides Foo()
:
class Derived : public Base
{
public:
void Foo(){std::cout << "this is the derived class";}
}
Then there is some other class that contains a list of Base
:
class OtherClass
{
public:
std::vector<Base> listOfBases; //note it's not std::list<Derived>
}
How do I cycle through the listOfBases
and call Foo()
for the Derived
class not the Base
class? Right now if I was to say listOfBases[i].Foo();
then this is the base class would print, but I want the overridden one from the Derived
class to print.
I could just make it a list of Derived
instead of Base
and that would fix it, but I'm going to be calling these inherited class all kinds of different things so I need a list of Base
.
So how do I call an overridden function from a list of its base class?
You need to use a list of Base*
(that is, pointers-to-Base), or preferably, std::unique_ptr<Base>
or std::shared_ptr<Base>
.
The reason for this is because of the C++ object model and copying. Derived classes must be at least as large as their base class (they can be the same size depending on the derived class being empty or not). Since C++ utilizes copying (or possibly moving in C++11) when adding items to a vector
, it must allocate enough space for n Base
objects. Since a vector
is almost always a wrapper around a simple array
, trying to add a (possibly differently sized) Derived
object into an array
of Base
objects is undefined behaviour.
Get a pointer to each base class, and then downcast it a Derived class. It's called downcast because in a UML diagram it is conventional to draw the base class above the derived class.
for ( auto q = listOfBases.begin(); q != listOfBases.end(); ++q )
{
Base* pBase = &(*q); // Get pointer to Base class. Can't downcast on object.
Derived* pDerived = dynamic_cast<Derived*>(pBase); // Downcast
pDerived->Foo(); // Call Foo() of Derived
}
Let's say I have an empty class with a virtual function:
class Base
{
public:
virtual void Foo(){std::cout << "this is the base class";}
}
Then I have a class that inherits from Base
and overrides Foo()
:
class Derived : public Base
{
public:
void Foo(){std::cout << "this is the derived class";}
}
Then there is some other class that contains a list of Base
:
class OtherClass
{
public:
std::vector<Base> listOfBases; //note it's not std::list<Derived>
}
How do I cycle through the listOfBases
and call Foo()
for the Derived
class not the Base
class? Right now if I was to say listOfBases[i].Foo();
then this is the base class would print, but I want the overridden one from the Derived
class to print.
I could just make it a list of Derived
instead of Base
and that would fix it, but I'm going to be calling these inherited class all kinds of different things so I need a list of Base
.
So how do I call an overridden function from a list of its base class?
You need to use a list of Base*
(that is, pointers-to-Base), or preferably, std::unique_ptr<Base>
or std::shared_ptr<Base>
.
The reason for this is because of the C++ object model and copying. Derived classes must be at least as large as their base class (they can be the same size depending on the derived class being empty or not). Since C++ utilizes copying (or possibly moving in C++11) when adding items to a vector
, it must allocate enough space for n Base
objects. Since a vector
is almost always a wrapper around a simple array
, trying to add a (possibly differently sized) Derived
object into an array
of Base
objects is undefined behaviour.
Get a pointer to each base class, and then downcast it a Derived class. It's called downcast because in a UML diagram it is conventional to draw the base class above the derived class.
for ( auto q = listOfBases.begin(); q != listOfBases.end(); ++q )
{
Base* pBase = &(*q); // Get pointer to Base class. Can't downcast on object.
Derived* pDerived = dynamic_cast<Derived*>(pBase); // Downcast
pDerived->Foo(); // Call Foo() of Derived
}
0 commentaires:
Enregistrer un commentaire