# USTRUCT and inheritance

**URL:** <https://forums.unrealengine.com/t/ustruct-and-inheritance/142421>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 28, 2020, 7:20pm UTC](https://forums.unrealengine.com/t/ustruct-and-inheritance/142421 "2020-04-28T19:20:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![KowardlyMan](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KowardlyMan](https://forums.unrealengine.com/u/KowardlyMan)\
**Post date:** [April 28, 2020, 7:20pm UTC](https://forums.unrealengine.com/t/ustruct-and-inheritance/142421/1 "2020-04-28T19:20:56Z")

</div>

Hello, I found out something very strange. A USTRUCT can inherit a struct, only if it is a base struct.  
For instance the following snippet does not work:

```auto

#pragma once

#include "CoreMinimal.h"

#include "FMinimumExample.generated.h"

struct TEDIUM_API FSubClassIntVector : public FIntVector
{
};

USTRUCT()
struct TEDIUM_API FMinimumExample : public FSubClassIntVector
{
    GENERATED_BODY()
};

```

This gives compile error : ‘struct’: Can’t find struct ‘FSubClassIntVector’. At the same time, if you directly extends from FIntVector works. Any idea why this limitation exists?  
Is there a way around it?

---

<div class="post-metadata">

**Author:** ![namrog84](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/namrog84/32/1160009_2.png) [@namrog84](https://forums.unrealengine.com/u/namrog84)\
**Post date:** [April 28, 2020, 9:55pm UTC](https://forums.unrealengine.com/t/ustruct-and-inheritance/142421/2 "2020-04-28T21:55:10Z")

</div>

Although this probably doesn’t answer the why. It’s probably just how the UHT tool is designed.

Here are 2 proposed alternatives.

As you said, your original example doesn’t work.

This works for me.

```auto

USTRUCT()
struct FSubClassIntVector : public FIntVector
{
    GENERATED_BODY()
};

USTRUCT()
struct FMinimumExample : public FSubClassIntVector
{
    GENERATED_BODY()
};

```

This leads me to believe UHT can’t handle multiple inheritance for things that everything isn’t a USTRUCTs.  
Which means if you want to have part of your game ‘inside UE4’ and some ‘outside UE4’ You totally can, but you just need to separate the layers more distinctly and not be 1/2 in and a 1/2 out. Either go fully in with USTRUCTS or have a clearly defined communication layer to the outside.

Alterantively,

If FSubClassIntVector is out of your control to edit. You can go with a composition approach instead. Then just pass off the internal struct at the appropriate place instead. It requires a slightly different approach in other areas depending on the project.  
This works for me too.

```auto

struct FSubClassIntVector : public FIntVector
{
};

USTRUCT()
struct FMinimumExample{

    GENERATED_BODY()

    FSubClassIntVector SubClassIntVector;
};

```

---

<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 30, 2020, 9:13am UTC](https://forums.unrealengine.com/t/ustruct-and-inheritance/142421/3 "2020-04-30T09:13:40Z")

</div>

This is by design, but you can get around it in special cases. Typically - _ **Reflection** _ requires that the primary type your inherit from must also be a reflected type. You can use multiple inheritance, but the additional types that you inherit from cannot be reflected types.

_ **However** _ there are cases where it’s legitimate to declare a new reflected type but also inherit from a non-reflected type - particularly for USTRUCT(). For obvious reasons you can’t expose this to the reflection system, but using the **#if CPP** pre-processor macro you can tell UHT to skip certain lines which will allow these cases to compile. This is how I am able to use an interface class for these two structs:

```auto

class IST_ComboBoxItem
{
public:
    virtual ~IST_ComboBoxItem() {}

    virtual FText GetDisplayText() const = 0;
};

USTRUCT(BlueprintType)
struct FOption_StringValue
#if CPP
    : public IST_ComboBoxItem
#endif
{};

USTRUCT(BlueprintType)
struct FOption_DiscreteValue
#if CPP
    : public IST_ComboBoxItem
#endif
{}

```

---

<div class="post-metadata">

**Author:** ![hogs9](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hogs9/32/946522_2.png) [@hogs9](https://forums.unrealengine.com/u/hogs9)\
**Post date:** [July 4, 2022, 6:10am UTC](https://forums.unrealengine.com/t/ustruct-and-inheritance/142421/4 "2022-07-04T06:10:41Z")

</div>

Sorry for resurrecting this thread, but thought I’d just add that this works for me in UE5. Not sure if it has been a recent addition, but here is my USTRUCT inheritance structure that works fine.

```auto
USTRUCT(BlueprintType)
struct FInventoryItem : public FTableRowBase { GENERATED_BODY() }

USTRUCT(BlueprintType)
struct FInventoryItemEquipment : public FInventoryItem { GENERATED_BODY() }

USTRUCT(BlueprintType)
struct FInventoryItemWeapon : public FInventoryItemEquipment { GENERATED_BODY() }

```

---

<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:** [July 4, 2022, 7:57am UTC](https://forums.unrealengine.com/t/ustruct-and-inheritance/142421/5 "2022-07-04T07:57:32Z")

</div>

That works fine because all of the types you are inheriting are reflected base types.

---

<div class="post-metadata">

**Author:** ![hogs9](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/hogs9/32/946522_2.png) [@hogs9](https://forums.unrealengine.com/u/hogs9)\
**Post date:** [July 4, 2022, 11:45pm UTC](https://forums.unrealengine.com/t/ustruct-and-inheritance/142421/6 "2022-07-04T23:45:49Z")

</div>

Ah that makes sense, looks like I misinterpreted the initial question. Thanks for replying!

---

<div class="post-metadata">

**Author:** ![axelkomair01](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/axelkomair01/32/88714_2.png) [@axelkomair01](https://forums.unrealengine.com/u/axelkomair01)\
**Post date:** [June 13, 2023, 8:15am UTC](https://forums.unrealengine.com/t/ustruct-and-inheritance/142421/7 "2023-06-13T08:15:41Z")

</div>

amazing thank you
