I didn't fully understand object slicing in C++. With the following example code two objects seem to receive the same processing, but polymorphism works for only one of them.
I'm using references and one of the objects doesn't seem to be sliced. I believe something must happen during the launch_ship function call, but I don't know exactly what is going wrong.
Here is the example code.
#include <iostream>
class SpaceShip
{};
class MilleniumFalcon: public SpaceShip
{};
class Pilot
{
public:
virtual void operate(SpaceShip&)
{
std::cerr << "Operating spaceship" << std::endl;
}
virtual void operate(MilleniumFalcon&)
{
std::cerr << "Cannot operate that spaceship!" << std::endl;
}
};
class Chewbacca: public Pilot
{
public:
virtual void operate(SpaceShip&)
{
std::cerr << "Don't want to operate that low spaceship!" <<
std::endl;
}
virtual void operate(MilleniumFalcon&)
{
std::cerr << "Operating the Millenium Falcon" << std::endl;
}
};
void launch_ship(Pilot& pilot, SpaceShip& ship)
{
pilot.operate(ship);
}
int main()
{
Chewbacca chewie;
MilleniumFalcon millenium;
launch_ship(chewie, millenium);
}
output : Don't want to operate that low spaceship!