Casting from a parent class to a TSubclassOf

I have this function to select a weapon

void AShooterCharacter::SelectWeapon(TSubclassOf<AWeapon> WeaponClass)
{
    if (CurrentWeapon)
    {
        CurrentWeapon->Destroy(true);
    }

    CurrentWeapon = GetWorld()->SpawnActor<AWeapon>(WeaponClass); 
    CurrentWeapon->AttachToComponent(Mesh1P, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), TEXT("GripPoint"));
}

However I want to add a check so that if CurrentWeapon is of the same subclass as WeaponClass, I just return. What would be the solution to this? I was thinking that it could be possible to try to cast CurrentWeapon to the WeaponClass, but I’m not sure how to do that, since WeaponClass is a TSubClassOf?

Tried this?

You probably want to do something like

if (CurrentWeapon && CurrentWeapon->GetClass() == WeaponClass)
{
    return;
}

This will ensure that you can still switch between “variants” of a weapon made using further subclasses, if you’re doing that sort of thing.

That did the trick, thank you! I’ve been trying to wrap my head around classes and TSubclassOf and their differences but at least this made it work!