Difference between casting and assigning to a class

Hi all, can anyone please let me know what the difference is between this:


for (TActorIterator<APortal> ActorItr(GetWorld()); ActorItr; ++ActorItr) 
{
if (ActorItr->GetOwner() != this) {
OtherPortal = Cast<APortal>(*ActorItr);
}
}

And this:


for (TActorIterator<APortal> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
if (ActorItr->GetOwner() != this) { 
OtherPortal = *ActorItr;
}
}

So basically to get the other portal:


OtherPortal = *ActorItr;

OR:


OtherPortal = Cast<APortal>(*ActorItr);

Any help is appreciated, thank you.

It depends what OtherPortal is defined as. Classes that share the same base class can use a pointer of that base type and be fine.

If I have 2 classes, Car and Boat, and both inherit from a class called Vehicle - then my Vehicle pointer can be assigned to either a Car or a Boat. However, if I want to turn that Vehicle pointer into a Car/Boat, I need to cast it. This is called upcasting (implicitly casting to a base/parent class) and downcasting (explicitly casting to a child class).

https://www.bogotobogo.com/cplusplus/upcasting_downcasting.php

OtherPortal is of class APortal so I don’t need to do any casting, thanks so much for the explanation its a whole lot clearer for me now :slight_smile: