Download

Inheritance of a third party pure abstract class for my plugin

I’m writing a plugin that integrates some third party code. It requires me to implement some methods from a pure abstract class but as far as I’ve read and seen, Unreal does not allow multiple inheritance.

This means that I’m ruled out from having a UObject that inherits from UObject and the third party pure abstract class (Let’s call it MyPureAbstractClass). It also seems to rule out wrapping it in a UInterface class as it would need to inherit from UInterface and MyPureAbstractClass.

Here’s an example of the class:


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

	virtual void myFunctionOne() = 0;
	virtual void myFunctionTwo() = 0;
	virtual void myFunctionThree( const int myInt,
		const double myDouble ) = 0;
};

So what is the proper way to integrate this non-Unreal class with Unreal so that I can override it?

It’s the other way around - you can multiple inherit just fine, but you’re only allowed a single UObject-derived base. In this case, if you’d tried the obvious solution you initially mentioned, you’d find it works fine:


UCLASS()
class UMyObject: public UObject, public MyPureAbstractClass
{
  ...

  // Implement MyPureAbstractClass method overrides
};

Hmm, that’s news to me; last time I tried it like that it didn’t work (but tried long ago)

Yeah it appears that if it worked in the past, it doesn’t work now. I end up getting an UnrealHeaderTool error for my class definition:


Missing '{' in 'Class'

I’m going to attempt a slightly messy way of wrapping up the pure abstract class in another class so that wrapper class can get away with just needing to inherit UObject or UInterface. If I succeed I’ll post my solution here soon.

It works, I’m using it myself frequently, as does the engine. See here for a randomly plucked example.

Post your code if you like.

Sorry about the late reply, I had a workaround (Albeit messy) where I encapsulated a class within a class in order to hide the inheritance (Can post code if anyone ever finds it useful).

After looking at the example you provided very carefully, here was an example of the code I had:


UCLASS()
class MYPROJECT_API UUnrealEntity : public UObject, public MyNamespace::MyPureAbstractClass
{
	GENERATED_BODY()
		
public:
        // Stuff
};

Compare that to the example class you provided:



UCLASS()
class UPaperTiledImporterFactory : public UFactory, public FReimportHandler
{
	GENERATED_UCLASS_BODY()
        // Etc, etc
};

**
The third party class that I need to inherit from is not prefixed with an “F”. **

After taking a chance that the subtle difference had something to do with it, I came across https://forums.unrealengine.com/showthread.php?60061-Unreal-trivia-What-does-the-F-prefix-on-classes-and-structs-stand-for&p=368952&viewfull=1#post368952.

So it looks like the requirement for multiple inheritance to be Unreal classes means that you need to have an external class’ class name prefixed with an “F” (Or more accurately, any class that won’t be looked after by the UObject garbage collector and can be multi-inherited as it’s an Unreal type), which is a little more work on my end. Here’s an example of what I ended up with using a class with a mix of virtual and normal methods (Ignoring pure abstract for now) that compiles, though I haven’t tested it’s use.


class FMyEntityClass : public MyNamespace::MyEntityClass
{
public:
	// Stuff
};

UCLASS()
class MYPROJECT_API UUnrealEntity : public UObject, public FMyEntityClass
{
	GENERATED_BODY()
		
public:
        // Stuff
};

It’s a a shame that I have to have a class in the middle just to use the right naming convention, but it’s workable.

I suspect it’s the namespacing that UHT is unable to handle, not the prefix - I use multiple inheritance with base classes which don’t follow UE4 naming conventions.

Did you try like this?



using MyNamespace::MyPureAbstractClass;

UCLASS()
class MYPROJECT_API UUnrealEntity : public UObject, public MyPureAbstractClass


That’s the good answer! Thanks