How to use TSubclassOf<>?

I’m a beginner for ue4 cpp,and my english is poor…so i can’t describe my problem clearly…just following the picture


and the code in official example is


actually,the code in official example doesn’t have “(USceneComponent*)”,maybe its auto cast,but in my code will worry ,so i add it.

thx very much!

TSubclass as the name implies is a *class *of USceneComponent in this case - not an instance. It’s almost certainly not what you want.

You also don’t need to use the C-Style cast (and you should generally never use C-Style casts in UE4). You don’t need it in this case becaues CreateDefaultSubobject is a template function and returns the type pre-casted. Any casting in UE4, especially with UObjects, should be handled by the static Cast<> function.

First of all, thx very much for replying to my primary question…but ,if i dont use


**,the code will be wrong…because of

**UStaticmeshcomponent* cant give to UScenecomponent***

…but in official example its right with no


Don’t forget to:

#include "Components/StaticMeshComponent.h"

Oh!! You are right! I just forget to import


But the code also can run with no warning…its too fake…
Thx very much.