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.
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?