This is a great question that hits at the heart of some of the trickier points of C++ inheritance. The confusion arises because of the difference between static types and dynamic types, as well as the way that C++ allocates storage for objects.
To begin, let's discuss the difference between static and dynamic types. Every object in C++ has a static type, which is the type of the object that is described in the source code. For example, if you try writing
Base* b = new Derived;
Then the static type of b
is Base*
, since in the source code that's the type you declared for it. Similarly, if you write
Base myBases[5];
the static type of myBases
is Base[5]
, an array of five Base
s.
The dynamic type of an object is the type that the object actually has at runtime. For example, if you write something like
Base* b = new Derived;
Then the dynamic type of b
is Derived*
, since it's actually pointing at a Derived
object.
The distinction between static and dynamic types is important in C++ for two reasons:
- Assignments to objects are always based on the static type of the object, never the dynamic type.
- Invocations of virtual functions only dispatch to the dynamic type if the static type is a pointer or reference.
Let's address each of these in turn.
First, one of the problems with the second version of the code is that you do the following:
Ninja ninja;
Monster monster;
Enemy enemies[2];
enemies[0] = monster;
enemies[1] = ninja;
Let's trace through what happens here. This first creates a new Ninja
and Monster
object, then creates an array of Enemy
objects, and finally assigns the enemies
array the values of ninja
and monster
.
The problem with this code is that when you write
enemies[0] = monster;
The static type of the lhs is Enemy
and the static type of the rhs is Monster
. When determining how to do an assignment, C++ only looks at the static types of the objects, never the dynamic types. This means that because enemies[0]
is statically typed as an Enemy
, it has to hold something precisely of type Enemy
, never any derived type. This means that when you do the above assignment, C++ interprets this to mean "take the monster
object, identify just the part of it that's an Enemy
, then copy that part over into enemies[0]
." In other words, although a Monster
is an Enemy
with some extra additions, only the Enemy
part of Monster
will be copied over into enemies[0]
with this line of code. This is called slicing, since you're slicing off part of the object and leaving behind just the Enemy
base portion.
In the first piece of code that you posted, you have this:
Ninja ninja;
Monster monster;
Enemy *enemies[2];
enemies[0] = &monster;
enemies[1] = &ninja;
This is perfectly safe, because in this line of code:
enemies[0] = &monster;
The lhs has static type Enemy*
and the rhs has type Monster*
. C++ legally allows you to convert a pointer to a derived type into a pointer to a base type without any problems. As a result, the rhs monster
pointer can be converted losslessly into the lhs type Enemy*
, and so the top of the object isn't sliced off.
More generally, when assigning derived objects to base objects, you risk slicing the object. It is always safer and more preferable to store a pointer to the derived object in a pointer to a base object type, because no slicing will be performed.
There's a second point here as well. In C++, whenever you invoke a virtual function, the function is only called on the dynamic type of the object (the type of the object that the object really is at runtime) if the receiver is a pointer or reference type. That is, if you have the original code:
Ninja ninja;
Monster monster;
Enemy enemies[2];
enemies[0] = monster;
enemies[1] = ninja;
And write
enemies[0].attack();
then because enemies[0]
has static type Enemy
, the compiler won't use dynamic dispatch to determine which version of the attack
function to call. The reason for this is that if the static type of the object is Enemy
, it always refers to an Enemy
at runtime and nothing else. However, in the second version of the code:
Ninja ninja;
Monster monster;
Enemy *enemies[2];
enemies[0] = &monster;
enemies[1] = &ninja;
When you write
enemies[0]->attack();
Then because enemies[0]
has static type Enemy*
, it can point at either an Enemy
or a subtype of Enemy
. Consequently, C++ dispatches the function to the dynamic type of the object.
Hope this helps!