# Template UClass or an other way ?

**URL:** <https://forums.unrealengine.com/t/template-uclass-or-an-other-way/142160>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 24, 2020, 4:50pm UTC](https://forums.unrealengine.com/t/template-uclass-or-an-other-way/142160 "2020-04-24T16:50:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SixtyOne](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SixtyOne](https://forums.unrealengine.com/u/SixtyOne)\
**Post date:** [April 24, 2020, 4:50pm UTC](https://forums.unrealengine.com/t/template-uclass-or-an-other-way/142160/1 "2020-04-24T16:50:52Z")

</div>

Hello dev, i would like to make something like that :

```auto

template <class T>
UCLASS()
class PROJECT_API AMyActor : public AActor
{
    GENERATED_BODY()

public:
    AMyActor();

    UPROPERTY(EditDefaultsOnly)
    T* MyComponent = nullptr;
}

AMyActor<T>::AMyActor()
{
    MyComponent = CreateDefaultSubobject<T>("MyComp");
}

UCLASS()
class PROJECT_API AMyChildActor : public AMyActor<USceneComponent>
{
    GENERATED_BODY()
}

```

obviously, he doesn’t compile. But, my questions :

Can we have template UClass ? If yes, how please ?  
Did you see a design pattern for make something like that or a tips ?

Thank’s for the read,  
Have a nice day !

---

<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:** [April 25, 2020, 9:03am UTC](https://forums.unrealengine.com/t/template-uclass-or-an-other-way/142160/2 "2020-04-25T09:03:36Z")

</div>

No, you can’t use templates with reflection or UObjects.

There are a handful of special templated containers in UE4 that support reflection such as TArray, TMap and TSet etc, but they all have special handling.

---

<div class="post-metadata">

**Author:** ![SixtyOne](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SixtyOne](https://forums.unrealengine.com/u/SixtyOne)\
**Post date:** [April 27, 2020, 9:48am UTC](https://forums.unrealengine.com/t/template-uclass-or-an-other-way/142160/3 "2020-04-27T09:48:38Z")

</div>

Ok thank’s 🙂

---

<div class="post-metadata">

**Author:** ![SixtyOne](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SixtyOne](https://forums.unrealengine.com/u/SixtyOne)\
**Post date:** [April 28, 2020, 8:57am UTC](https://forums.unrealengine.com/t/template-uclass-or-an-other-way/142160/4 "2020-04-28T08:57:37Z")

</div>

I have something, but for this moment we can’t edit component in editor, but it’s work.

```auto

class UMyGenericClass
{
public:
    UMyGenericClass()
    {}

    UMyGenericClass(AActor* _actor)
    {}
};

template<class T>
class UMyTemplateClass : public UMyGenericClass
{
public:
    UMyTemplateClass(AActor* _actor)
        : UMyGenericClass() 
    {
        if (_actor)
        {
            MyComponent = _actor->CreateDefaultSubobject<T>("MyComp");
        }
    }

public:
    T* MyComponent = nullptr;
};

template<class T>
struct STag { using type=T; };

UCLASS()
class USETEMPLATE_API AMother : public AActor
{
    GENERATED_BODY()

public:    
    // Sets default values for this actor's properties
    AMother();
    template<class T>
    AMother(STag<T> _tag);

public:
    UMyGenericClass GenericClass;
};

template<class T>
AMother::AMother(STag<T> _tag)
{
    GenericClass = UMyTemplateClass<T>(this);
    PrimaryActorTick.bCanEverTick = true;
}

```

and in my .cpp

```auto

// Sets default values
AMother::AMother()
    : AMother(STag<USceneComponent>{})
{
}

```

if you have any question, you can mp me
