# how to pass dynamic class to generic function

**URL:** <https://forums.unrealengine.com/t/how-to-pass-dynamic-class-to-generic-function/1637200>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, dynamic, template, CPP\
**Created:** [January 23, 2024, 6:12pm UTC](https://forums.unrealengine.com/t/how-to-pass-dynamic-class-to-generic-function/1637200 "2024-01-23T18:12:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![user\_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@user\_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8](https://forums.unrealengine.com/u/user_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8)\
**Post date:** [January 23, 2024, 6:12pm UTC](https://forums.unrealengine.com/t/how-to-pass-dynamic-class-to-generic-function/1637200/1 "2024-01-23T18:12:05Z")

</div>

```auto
AX2BaseActor::AX2BaseActor(TSubclassOf<UMeshComponent> MeshClass = UStaticMeshComponent::StaticClass())
{
	// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	Mesh = CreateDefaultSubobject<MeshClass.Get()->GetClass()>(TEXT("Mesh"));
	Mesh->SetMobility(EComponentMobility::Movable);
	RootComponent = Mesh;

	Item = CreateDefaultSubobject<UX2Item>(TEXT("Item"));
}

```

in the above code, i have a ClassType parameter called MeshClass, and i want to create its instance by calling

```auto
	Mesh = CreateDefaultSubobject<MeshClass.Get()->GetClass()>(TEXT("Mesh"));

```

but this won’t work, error is pasted below:

```auto
note: 'UObject::CreateDefaultSubobject': invalid template argument for 'TReturnType', type expected
  [7/14] Compile [x64] X2Axe.cpp

```

i do a lot search, but it seems that no clear answer. since MeshClass itself is a class type reference, it should be able to be passed as template parameter in generic functions.  
thanks very much.

---

<div class="post-metadata">

**Author:** ![Shmoopy1701](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/shmoopy1701/32/1194536_2.png) [@Shmoopy1701](https://forums.unrealengine.com/u/Shmoopy1701)\
**Post date:** [January 23, 2024, 6:45pm UTC](https://forums.unrealengine.com/t/how-to-pass-dynamic-class-to-generic-function/1637200/2 "2024-01-23T18:45:49Z")

</div>

Not sure this is possible, or if I’d want to see it in any code, but I’d be interested to see the answer.

What is the type of “Mesh”? Also what is the use case?

---

<div class="post-metadata">

**Author:** ![UnrealEverything](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UnrealEverything](https://forums.unrealengine.com/u/UnrealEverything)\
**Post date:** [January 23, 2024, 8:34pm UTC](https://forums.unrealengine.com/t/how-to-pass-dynamic-class-to-generic-function/1637200/3 "2024-01-23T20:34:41Z")

</div>

You can use the non-templated `CreateDefaultSubobject` version (which is called by the templated one) that takes additional `const UClass* ReturnType, const UClass* ClassToCreateByDefault` parameters. `ReturnType` would be `UMeshComponent::StaticClass()` and `ClassToCreateByDefault` would be `*MeshClass` in your example.

If you’re using `FObjectInitializer` you can call `SetDefaultSubobjectClass` to set the override class to create instead. This is typically used directly in the subclass.

---

<div class="post-metadata">

**Author:** ![user\_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@user\_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8](https://forums.unrealengine.com/u/user_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8)\
**Post date:** [January 24, 2024, 12:13am UTC](https://forums.unrealengine.com/t/how-to-pass-dynamic-class-to-generic-function/1637200/4 "2024-01-24T00:13:56Z")

</div>

i want to create a base actor class, which can manage static and skeletal Mesh in one single property. this will help simply the code somehow. but after doing a lot lot search, i think this may not be possible, sad. thank u anyway.

---

<div class="post-metadata">

**Author:** ![user\_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@user\_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8](https://forums.unrealengine.com/u/user_3efc87921bcb4110447d2e17e4d7044ea0722e55da9367bcdafdd8)\
**Post date:** [January 24, 2024, 2:07am UTC](https://forums.unrealengine.com/t/how-to-pass-dynamic-class-to-generic-function/1637200/5 "2024-01-24T02:07:15Z")

</div>

thank u very much, this is what i needed for now. here is the final code

```auto
Mesh = Cast<UMeshComponent>(CreateDefaultSubobject(TEXT("Mesh"), UMeshComponent::StaticClass(), MeshClass, true, false));

```

sadly i’m not familiar with the forum, i think there is a button to mark as answer, i can not found it out.

but this is not a perfect solution, maybe i will encounter a similar scene, but without lucky to find another CreateDefaultSubobject to help. it do helps for the current problem.
