Forward Declaration Question


class UCapsuleComponent;


UCLASS()
class TOONTANKS_API APawnBase : public APawn
{
	GENERATED_BODY()

private:

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess = "true"))
	UCapsuleComponent* CapsuleComp;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess = "true"))
	UStaticMeshComponent* BaseMesh;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess = "true"))
	UStaticMeshComponent* TurretMesh;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Components", meta=(AllowPrivateAccess = "true"))
	USceneComponent* ProjectileSpawnPoint;


public:
	// Sets default values for this pawn's properties
	APawnBase();
	
};

In PawnBase.h, we have UCapsuleComponent and UStaticMeshComponent and USceneComponent, I wonder why we only have UCapsuleComponent class in the forward declaration, and no need to declare for the other two class?

Thanks in advance!

You can either have a class in forward declaration or you can simply #include the header file of the class -

class UCapsuleComponent;

Or,

#include "Components/CapsuleComponent.h"
// Components/CapsuleComponent.h is the path of the class.

Both methods will work just fine.

So, I haven’t done this Toon tanks section, but I think the instructor, instead of forward declaring, he just #included the path of the class.

Thanks for your reply!
I wonder why we only need to declare the UCapsuleComponent but don’t need to declare the other two, USceneComponent and UStaticMeshComponent?

I haven’t seen the instructor’s source code. Maybe he just #included the path of the class in the header file. That’s the alternate way of doing this. You can either forward declare or #include the path of the class. Either way will work just fine.

He didn’t include in cpp file and also didn’t forward declare it in header file. He only include the capusleComponent.h in cpp file. :thinking:

It would come from one of the includes that are already there.

Do UStaticMeshComponent and USceneComponent come from #include “GameFramework/Pawn.h”?
How can I know if it comes from the include?

Do UStaticMeshComponent and USceneComponent come from #include “GameFramework/Pawn.h”?

I don’t know, there’s too many includes. But it has to come in somewhere otherwise it wouldn’t compile.

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

Privacy & Terms