How does casting work? C++

Hello,
I’m trying to understand what casting is doing. I’m primarily looking at example code to get a better understanding of it. I can’t quite grasp why or when you would use Cast.

Here is the example I would like help understanding:

FString ASGameMode::InitNewPlayer(class APlayerController* NewPlayerController, const TSharedPtr<const FUniqueNetId>& UniqueId, const FString& Options, const FString& Portal)
{
	FString Result = Super::InitNewPlayer(NewPlayerController, UniqueId, Options, Portal);
	

	ASPlayerState* NewPlayerState = Cast<ASPlayerState>(NewPlayerController->PlayerState);
       // Do stuff

	return Result;
}

Now I assume cast is necessary because of the Super::InitNewPlayer();
But what exactly is happening in this code? Why do we need to cast anything in the first place and not just use our class ASPlayerController instead of the default PlayerController (Which I assume is because of InitNewPlayer)

Any assistance would be awesome!
Thanks

A cast checks to see whether the object is of the given class and, if so, treats it like one. (if not, it returns NULL). In this case, it’s needed since the PlayerState property is a APlayerState*, not an ASPlayerState*, so you must tell the program “trust me here - it’s an ASPlayerState”.