# TSubclassOf upward cast

**URL:** <https://forums.unrealengine.com/t/tsubclassof-upward-cast/42020>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [September 17, 2015, 1:19pm UTC](https://forums.unrealengine.com/t/tsubclassof-upward-cast/42020 "2015-09-17T13:19:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![scw000000](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@scw000000](https://forums.unrealengine.com/u/scw000000)\
**Post date:** [September 17, 2015, 1:19pm UTC](https://forums.unrealengine.com/t/tsubclassof-upward-cast/42020/1 "2015-09-17T13:19:48Z")

</div>

I have two classes A and B, B is inherited from A:

UCLASS()  
class A  
{  
…  
}

UCLASS()  
class B:A  
{  
…  
}

now I’m having a function take a parameter of TSubclassOf\<class A\>  
//declare of function  
foo( TSubclassOf\<class A\> varClass )

However, I want to pass the parameter as TSubclassOf\<class B\>

//declare of varClass  
UPROPERTY(…)  
TSubclassOf\<class B\> varClass

//try to call foo by this variable  
foo( varClass )

the compiler will send err:  
error C2440: ‘initializing’ : cannot convert from ‘B\*’ to ‘A\*’

How should I avoid this? Since I found it really convenient of declaring varClass as TSubclassOf\<\> instead of UClass\*.  
I will prefer using TSubclassOf\<\> if I can solve this question.

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [September 17, 2015, 1:22pm UTC](https://forums.unrealengine.com/t/tsubclassof-upward-cast/42020/2 "2015-09-17T13:22:42Z")

</div>

Will this help at all?

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

---

<div class="post-metadata">

**Author:** ![scw000000](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@scw000000](https://forums.unrealengine.com/u/scw000000)\
**Post date:** [September 17, 2015, 1:27pm UTC](https://forums.unrealengine.com/t/tsubclassof-upward-cast/42020/3 "2015-09-17T13:27:52Z")

</div>

No, what I’m trying to do is pass it as a parameter.  
I don’t want to access any of its variable or function.

The best way I can thought is to modify the declaration of function, while keeping the variable type.

---

<div class="post-metadata">

**Author:** ![iniside](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/iniside/32/866315_2.png) [@iniside](https://forums.unrealengine.com/u/iniside)\
**Post date:** [September 17, 2015, 5:49pm UTC](https://forums.unrealengine.com/t/tsubclassof-upward-cast/42020/4 "2015-09-17T17:49:56Z")

</div>

You can’t really cast TSubclassOf\<\> because it’s still pointer to UClass\*, not YourClass\*, and YourClass does not derive at any point from UClass.

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [September 17, 2015, 6:08pm UTC](https://forums.unrealengine.com/t/tsubclassof-upward-cast/42020/5 "2015-09-17T18:08:49Z")

</div>

It should work completely fine, since any TSubclassOf can be implicitly converted to UClass\*. I just did a quick test and got no errors. In fact a quick look at the docs shows there is even a templated copy-constructor taking any TSubclassOf argument.

I’m guessing you just have a problem with your class declarations. Perhaps at the point you call the function one of the classes has only been forward declared or something?

---

<div class="post-metadata">

**Author:** ![scw000000](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@scw000000](https://forums.unrealengine.com/u/scw000000)\
**Post date:** [September 17, 2015, 11:26pm UTC](https://forums.unrealengine.com/t/tsubclassof-upward-cast/42020/6 "2015-09-17T23:26:10Z")

</div>

> [@kamrann;381300](#):
>
> It should work completely fine, since any TSubclassOf can be implicitly converted to UClass\*. I just did a quick test and got no errors. In fact a quick look at the docs shows there is even a templated copy-constructor taking any TSubclassOf argument.
> 
> I’m guessing you just have a problem with your class declarations. Perhaps at the point you call the function one of the classes has only been forward declared or something?

yes, you are right!

Got no error at all if I include the header file.

Really appreciate your help.
