Super::somefunction() usage

I have a question about using Super::something(). I’m following a FPS tutorial from ue4 documentation, and there is a point where I’m declaring virtual DrawHUD() function override in the header, and then, in the declaration, I’m using Super::DrawHUD(). From what I read, Super:: is supposed to call a base DrawHUD(), even if we have a child that overrides some of its functions. So, what’s the point of declaring virtual and overrided function if we will still call a base one? Ordo I understand something wrong?

public:
    // Primary draw call for the HUD.
    virtual void DrawHUD() override;
void AFPSHUD::DrawHUD()
{
    Super::DrawHUD();

    if (CrosshairTexture)
    {
        // Find the center of our canvas.
        FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f);

        // Offset by half of the texture's dimensions so that the center of the texture aligns with the center of the Canvas.
        FVector2D CrossHairDrawPosition(Center.X - (CrosshairTexture->GetSurfaceWidth() * 0.5f), Center.Y - (CrosshairTexture->GetSurfaceHeight() * 0.5f));

        // Draw the crosshair at the centerpoint.
        FCanvasTileItem TileItem(CrossHairDrawPosition, CrosshairTexture->Resource, FLinearColor::White);
        TileItem.BlendMode = SE_BLEND_Translucent;
        Canvas->DrawItem(TileItem);
    }
}

Designing a good, clean API is difficult, especially in a project as large as Unreal.

calling a base method in an override method is perfectly legal, however some people consider it as a code smell, because it requires the programmer some extra knowledge of when to call base method(at beginning? at end? anywhere?) and whether it is necessary or not.

On the forum you can find a lot of threads where the main problem was forgetting to call Super ::

One solution is NVI (non-virtual interface pattern), but it is also not a silver bullet :slight_smile:

1 Like