# How to properly handle inheritance changes?

**URL:** <https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466>\
**Category:** C++\
**Tags:** unreal-engine, UE5-0, Scripting, question\
**Created:** [April 20, 2022, 10:19am UTC](https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466 "2022-04-20T10:19:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheEctenicForce](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheEctenicForce](https://forums.unrealengine.com/u/TheEctenicForce)\
**Post date:** [April 20, 2022, 10:19am UTC](https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466/1 "2022-04-20T10:19:01Z")

</div>

Hello,

Say I have a pawn class Player (extending pawn) and after some time I decide to add an Enemy (also extending pawn). Then I notice, they have many common qualities and I could use some abstraction, so I’d like to create a parent class.

What is the right way to do it if…  
A) I want the parent class to be just an interface that cannot be instantiated?  
… I would probably just create raw c++ header, but I am not sure if I wanted to use features like UFUNCTION it is the right thing to do.

B) The parent can be instantiated (say it is another pawn type).  
… Would I break something if I just change the parent class? How about the generated code?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [April 20, 2022, 10:38am UTC](https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466/2 "2022-04-20T10:38:43Z")

</div>

If you declare any pure virtual methods in a parent class it cannot be instantiated. (make a virtual method “=0”). You can also have friend classes and multiple inheritance - creating smaller classes for the shared code which can be reused between different classes:

```auto
class firstClass {
public:
	int32 firstVar;
	virtual int32 firstMethod()=0;
	int32 firstClassTask() { return 32; }
};

class secondClass {
public:
	int32 secondVar;
	virtual int32 secondMethod()=0;
	int32 secondClassTask() { return 42; }
};

class multiClass : public UObject,public firstClass,public secondClass {
public:
	int32 multiVar;
	virtual int32 firstMethod() { return firstVar+secondClassTask(); }
	virtual int32 secondMethod() { return secondVar+firstClassTask(); }
};

```

---

<div class="post-metadata">

**Author:** ![TheEctenicForce](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheEctenicForce](https://forums.unrealengine.com/u/TheEctenicForce)\
**Post date:** [April 20, 2022, 10:44am UTC](https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466/3 "2022-04-20T10:44:19Z")

</div>

Yes, that is what I meant by the raw c++, but is it ok to declare the pure virtusl function as UFUNCTION() for example?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [April 20, 2022, 11:01am UTC](https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466/4 "2022-04-20T11:01:40Z")

</div>

Kind of, check out this thread:

> [@How do I implement Pure Virtual Methods?](https://forums.unrealengine.com/t/how-do-i-implement-pure-virtual-methods/280323):
>
> Hi, I’m wondering why if writing pure virtual methods in my abstract base classes is supported? Currently I’ve got a class that inherits from actor and I’ve marked it as UCLASS(abstract) with a pure virtual method. I then have a class that inherits from this class, and implements the pure virtual method. When I compile this I get the following error. Only functions in the second interface class can be declared abstract. Does that mean if I want to use pure virtual methods in my abstract class…

---

<div class="post-metadata">

**Author:** ![TheEctenicForce](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheEctenicForce](https://forums.unrealengine.com/u/TheEctenicForce)\
**Post date:** [April 20, 2022, 12:23pm UTC](https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466/5 "2022-04-20T12:23:44Z")

</div>

Hmm that’s nice, I have noticed the examples with the abstract uclass derive from some Uobject, is it necessary for creating UFUNCTIONs? If yes, what do I do about the Diamond problem (A extends P, B extends P, C extends A & B)?

---

<div class="post-metadata">

**Author:** ![RecourseDesign](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/recoursedesign/32/1194503_2.png) [@RecourseDesign](https://forums.unrealengine.com/u/RecourseDesign)\
**Post date:** [April 20, 2022, 8:18pm UTC](https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466/6 "2022-04-20T20:18:24Z")

</div>

Yes you’ll need the parent to be derived from UObject and the children cannot have other UObject based classes multiple-inherited - but if you create child classes derived from the parent they can have UFUNCTIONS etc and you can use those children as parents to your practical child classes - if that makes sense…

---

<div class="post-metadata">

**Author:** ![TheEctenicForce](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheEctenicForce](https://forums.unrealengine.com/u/TheEctenicForce)\
**Post date:** [April 21, 2022, 7:58am UTC](https://forums.unrealengine.com/t/how-to-properly-handle-inheritance-changes/534466/7 "2022-04-21T07:58:41Z")

</div>

Yes, I have figured out the way to do it is probably using UINTERFACE, which unfortunately cannot directly implement any functions, but it provides sufficient abstraction.

Also, I have tried just changing the parent (class A : public APawn to class A : public MyNewParent), which pretty quickly crashed the engine, but after a clean rebuild - which I assume regenerated the files - it seems to be fine.
