Hello again! In this video, we are going to look at static type and dynamic type.
In the last video, we were looking at this "pShape", which is declared as pointed to a Shape.
But the actual address is the address of a Circle object and Circle is a child class of Shape.
So what actually is the type of pShape?
Is it a pointer to Shape, or pointer to Circle?
And the correct answer is both! In C++, variables actually have two types: the static type and the dynamic
type.
So let's have a quick look at this.
The static type is the one that is used in the declaration of the variable, so that is what the compiler
sees.
So we have int, pointer to int, Circle, pointer to Circle, reference to Circle and so on.
In the case of our pShape, this is declared as a pointer to a Shape.
So the static type is pointer to Shape. And similarly for reference, the static type is reference
to Shape.
And similarly, for references, the static type is reference to Shape.
The dynamic type depends on the variable that is actually in memory. And usually that is going to be the
same as the declaration.
So if we have an int, there is going to be an int object in memory.
If we have a pointer to int, that's going to be pointing to an int object, and so on.
However, if we have a pointer or reference to base class, it can be different. With our pShape,
the actual object is a Circle.
So the dynamic type of pShape is pointer to Circle. And for a reference, that would be reference
to Circle.
C++ is very, very keen on static typing! The compiler uses it for type checking and deciding which
function to call.
So this means there is less work to be done
at run time, so the program runs faster, and also it makes it easier to optimize the code.
The dynamic type is only used when we have a pointer or reference to a base class. In that case, the
compiler does not decide which member function is called.
Instead, the program will choose the function at run time, and it will choose the correct one for the object
that is in memory.
To do this, we need to declare the member function as virtual in the base class.
So, in the Shape class, we put "virtual", before we declare the function.
And then, when we call it through the pointer to Shape, the compiler will not get to decide that this
is a member function of Shape.
Instead, the program will decide that this is a Circle object.
So it is going to call the Circle version of draw().
So let's quickly try this out.
This is almost the same program as we had before, with just one small change.
So we have our vector of pointers to Shape objects.
We push a pointer to Circle onto it, and then we iterate through the elements and we call the draw() member function.
Before, we were using the static type.
So the compiler always choose the Shape version of draw().
We have now declared the draw() member function virtual.
So the program should decide that we have a Circle object, and it is going to call the Circle version of
draw().
So there it is. We have actually called the Circle version.
So the pointer to Shape has the static type of pointer to Shape, but the dynamic tight is pointer
to a Circle. And that called the Circle member function.
Okay, so that is it for this video.
I will see you next time.
But until then, keep coding!