Sorry I thought that’s what you were saying. Though if you’re strictly talking about polymorphism/inheritance then yes. Because
int x = 2.3;
is an implicit cast from double
to int
.
I believe what you’re asking is about upcasting/downcasting. Given the hierarchy:
AActor
|- APawn
|- ACharacter
Casting from ACharacter
to APawn
is an upcast whereas the other way around APawn
to ACharacter
is a downcast. Upcasts are always safe to do ACharacter
is APawn
however the other way round might not be - which is why there’s an if statement in the example as it can and does fail in one instance.
If you have a variable that is of type APawn*
, it may or may not actually be ACharacter
.
e.g. With our ATankPlayerController
it will always be controlling an ATank
so it’s a safe assumption to make and personally I wouldn’t bother checking if the cast failed. (This is not true for ATankAIController
later on).