c++ interface not forcing implementation

So I created a grabbable object interface with a simple function named grab in it. I then created an actor class that included the header for my Grabbable interface, then inherited from IGrabbable in my actor class. I switched over to the Unreal editor to compile. I realized I didn’t actually implement the Grab function from my interface, but it compiled just fine.

Not sure if this is a stupid question but, I was always under the impression that interfaces force you to have an implementation of the function. Am I missing something?

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "GrabbableInterface.generated.h"

// This class does not need to be modified.
UINTERFACE(MinimalAPI)
class UGrabbableInterface : public UInterface
{
	GENERATED_BODY()
};

/**
 * 
 */
class RUNTIME_API IGrabbableInterface
{
	GENERATED_BODY()

	// Add interface functions to this class. This is the class that will be inherited to implement this interface.
public:

	UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "Grabbable Object Interface Functions")
		void Grab();
};

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "GrabbableInterface.h"
#include "TestGrabActor.generated.h"

UCLASS()
class RUNTIME_API ATestGrabActor: public AActor , public IGrabbableInterface
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ATestGrabActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	
};
1 Like

First few basics facts:

  1. C++ don’t formally support interfaces but multi parenting instead which can work same way as interface, but for C++ “interface” is just normal class
  2. UE4 implmentation of interfaces don’t force you to implement it’s functions, if called they just do nothing if there no implementation. This is due to message like design of interfaces in Blueprints

That said in C++ you can force programmer to implement function by declaring function as virtual and don’t define (put code on it) on interface. This is standard for to emulate abstract classes in C++ too. you can read about it here:

Also you should declare all your interface functions as virtual, or else you only hiding function (type of variable dictates on which class function is executed) not overriding them.

Ah thats exactly the awnser i wwas looking for. Thanks for clearing that up for me.