# Casting from a parent class to a TSubclassOf

**URL:** <https://forums.unrealengine.com/t/casting-from-a-parent-class-to-a-tsubclassof/503067>\
**Category:** C++\
**Tags:** unreal-engine, question, UE4-27\
**Created:** [March 1, 2022, 11:48am UTC](https://forums.unrealengine.com/t/casting-from-a-parent-class-to-a-tsubclassof/503067 "2022-03-01T11:48:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![friberger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@friberger](https://forums.unrealengine.com/u/friberger)\
**Post date:** [March 1, 2022, 11:48am UTC](https://forums.unrealengine.com/t/casting-from-a-parent-class-to-a-tsubclassof/503067/1 "2022-03-01T11:48:47Z")

</div>

I have this function to select a weapon

```auto
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?

---

<div class="post-metadata">

**Author:** ![HAF-Blade](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/haf-blade/32/88718_2.png) [@HAF-Blade](https://forums.unrealengine.com/u/HAF-Blade)\
**Post date:** [March 1, 2022, 1:10pm UTC](https://forums.unrealengine.com/t/casting-from-a-parent-class-to-a-tsubclassof/503067/2 "2022-03-01T13:10:25Z")

</div>

Tried this?

> [@Best way to check class type in C++](https://forums.unrealengine.com/t/best-way-to-check-class-type-in-c/396569):
>
> Lets take a look at the below snippet of code: //Check if there's a valid object to grab if (actorHit != NULL) { //Retain a reference to the owning character and the grabbed item. myOwningCharacter = Cast\<ABaseCharacter\>(actorThatIsGrabbing); myGrabbedItem = Cast\<ABaseItem\>(actorHit); if(Cast\<ABaseWeapon\>(myGrabbedItem) != NULL) { this-\>GrabWeapon(myGrabbedItem); } else { this-\>GrabItem(myGrabbedItem); } } As you can see, we are checking to see if myGrabbedItem is …

---

<div class="post-metadata">

**Author:** ![Zeblote](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zeblote](https://forums.unrealengine.com/u/Zeblote)\
**Post date:** [March 1, 2022, 1:47pm UTC](https://forums.unrealengine.com/t/casting-from-a-parent-class-to-a-tsubclassof/503067/3 "2022-03-01T13:47:07Z")

</div>

You probably want to do something like

```auto
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.

---

<div class="post-metadata">

**Author:** ![friberger](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@friberger](https://forums.unrealengine.com/u/friberger)\
**Post date:** [March 1, 2022, 1:51pm UTC](https://forums.unrealengine.com/t/casting-from-a-parent-class-to-a-tsubclassof/503067/4 "2022-03-01T13:51:36Z")

</div>

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!
