Reputation:
What is the Super:: and why do we use it?
I am new to Unreal Engine. I searched a few and found that we use Super:: with override functions. In this way, we call the base class function. Why do we want to do this? Could someone explain properly, please?
Upvotes: 0
Views: 1413
Reputation: 8523
Your question is about the basic C++.
In short: Super is just the parent class.
The class whose properties are inherited by a subclass is called Base Class or Superclass
The class that inherits properties from another class is called a Subclass or Derived Class.
For example in Unreal, when you called the:
Super::BeginPlay()
It calls the parent class’s BeginPlay() method.
From the In the Parent class's perspective it's a virtual declared method like this:
virtual void BeginPlay()
If you have a Base Class where you defined a virtual function and gave it some logic in it, and you create a child class from it where you override this function, you can make sure that the Base Class code is still called by calling Super::YourFunction().
That's it.
Upvotes: 1
Reputation: 4037
Super
is a typedef
for the base class of your current class. It is part of the GENERATED_BODY()
and will be added by the UnrealHeaderTool
alongside ThisClass
.
In the following example, I define a class UYourClass
that inherits from UObject
, the base object for a lot of Unreals classes.
class UYourClass : public UObject
{
GENERATED_BODY()
};
The GENERATED_BODY()
will contain the following code:
typedef UYourClass ThisClass;
typedef UObject Super;
When you want to call an implementation for an overriden behavior of your base class, you do not need to write the base typename, but instead can just use Super
. And when accessing the own class, you can also use ThisClass
. It makes the code a little shorter and more portable, e.g. when you use macros to generate code.
In the following scenario, we inherit from AActor
and override its BeginPlay
method, so we can initialize our own object. But because AActor
also does stuff in its BeginPlay
implementation (e.g. calling the node "Begin Play" in blueprints), we have to call it as well, otherwise the behavior is faulty.
class ABuilding : public AActor
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
}
void ABuilding::BeginPlay()
{
Super::BeginPlay();
// Do your own stuff here
}
That's as far as I am willing to describe it. Everything beyond this point is just "C++ works like that" or "OOP works like that" and that's not suitable for this site.
Upvotes: 4