"class" in declaration

I got this working, but I’m not really sure WHY it’s working… I set up my C++ Character and used the following to set up the UInputComponent:

BaseCharacter.h


virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;


BaseCharacter.cpp


void ABaseCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    // Set up "movement" bindings.
    PlayerInputComponent->BindAxis("MoveForward", this, &ABaseCharacter::MoveForward);
    PlayerInputComponent->BindAxis("MoveRight", this, &ABaseCharacter::MoveRight);

    // Set up "look" bindings
    PlayerInputComponent->BindAxis("Turn", this, &ABaseCharacter::AddControllerYawInput);
    PlayerInputComponent->BindAxis("LookUp", this, &ABaseCharacter::AddControllerPitchInput);
}

Then I wanted to create a subclass of BaseCharacter:

SubCharacter.h


virtual void SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) override;


SubCharacter.cpp


void AJumperCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    // New action bindings go here
}

The difference is in the BaseCharacter.h the parameters include “**class **UInputComponent” whereas the SubCharacter.h only needs “UInputComponent”. Could someone please explain what’s going on there? I’m glad it works, but it’d be good to know why.

Cheers :slight_smile:

“class” is only used for declaring classes. It is forward declaring the class in the function parameters, which is definitely a confusing way to do it. You could also simply write:


class UInputComponent;

in BaseCharacter.h before the SetupPlayerInputComponent declaration.

I’m assuming the SubCharacter class doesn’t need the declaration of UInputComponent because it was already forward declared in the parent class. I’m not familiar enough with C++ to say for sure.

See this stackoverflow:
https://stackoverflow.com/questions/…tion-parameter

Thanks very much Kelso. Makes sense.