[HELP] What does Super do? Unreal C++

Hello,

I’m still not really sure what super:: does. I understand it’s a class and that it’s somehow related to inheritance, but anything more than that and I’m lost…
So what does super do?
Here’s the code that I’m especially confused about:

void UPositionReporter::BeginPlay()
{
	Super::BeginPlay(); // this line right here!

	UE_LOG(LogTemp, Warning, TEXT("Position report reporting for duty on Chair!")); // this is a macro
	
}

Thanks!
Enrico

It’s a type alias to the super class aka parent class. So for UPositionReporter that would be

using Super = UActorComponent;

So Super::BeginPlay() calls the parent class’ BeginPlay

@DanM, Ok. So all Super is is a shorter way of saying that BeginPlay() belongs to the UActorComponent class? If this was the case why wouldn’t unreal just say: UActorComponent::BeginPlay()?
Also, how does this relate to inheritance? Ben said something about how Super runs something first in the chain of inheritance? What does this mean?

Thanks,
Enrico

It’s to call the parent class’ function. I’ll give a code example at the end

So you don’t need to concern yourself with what the parent class is when writing these.


Code example

#include <iostream>

class Base
{
public:
    virtual void Foo() 
    {
        std::cout << "Base\n";
    } 
};
class Derived : public Base
{
public:
    using Super = Base;
    virtual void Foo() override
    {
        //uncomment the next line
        //Super::Foo();
        std::cout << "Derived\n";
    }
};

int main()
{
    Derived D;
    D.Foo();
}

https://godbolt.org/z/L_z-19

If you uncomment the Super::Foo() line you will see the output has changed.
Think of it this way, with the call to the super class version you are extending the functionality whereas without you are completely replacing the functionality.

1 Like

@DanM, sorry for the long response time … was applying to uni. Your response was very clear and thanks for the example. Some questions:

  1. Firstly, what do you mean by “functionality?”
  2. Secondly, BeginPlay shows up in two places:
void UGrabber::BeginPlay() // what is the difference between this BeginPlay()
{
	Super::BeginPlay(); // and this BeginPlay()?
}

I know they are both methods of classes (and that one is the method of UGrabber while the other is the method of UActorComponent) but is there any difference in what they do/how they work/etc.? Might I be overthinking this?

  1. Thirdly, Super is used in more than one place. How does that work? Is Super a big class with other classes inside it?
void UGrabber::BeginPlay()
{
	Super::BeginPlay(); // here and...
}

// Called every frame
void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction); // ...here
}

Finally, to make sure I understand your response, here’s my summary. Please tell me what’s right/wrong.
Super is just a class. BeginPlay() is one of the functions of that class that already has some functionality included because of Unreal Engine code.

Thank you!
Enrico

  1. As in the english definition of functionality
    int square(int n)
    {
        return n* n;
    }
    
    This has the functionality of squaring the input.
  2. First one defines the function UGrabber::BeginPlay the second one calls the parent class’ BeginPlay (see the godbolt link)
  3. It’s just an alias for the parent class so specifically for UGrabberComponent it would be
    using Super =  UActorComponent;
    
    You could replace all uses of Super with UActorComponent for UGrabberComponent. i.e.
    void UGrabber::BeginPlay()
    {
        UActorComponent::BeginPlay();
    }
    // Called every frame
    void UGrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
    {
        UActorComponent::TickComponent(DeltaTime, TickType, ThisTickFunction);
    }
    
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms