Add a component to an Actor during game play

Hi!

In blueprint I’m using this code:


To add a Static Mesh Component to an Actor.

How can I do it in C++?

I think this code is not going to work:

void ATFallingBlockActor::BuildCube(FLinearColor CubeColour)
{
	CubeCounter++;

	TObjectPtr<UTBlockCubeStaticMeshComponent> Cube =
		CreateDefaultSubobject<UTBlockCubeStaticMeshComponent>(FName(*FString::Printf(TEXT("Cube%02d"), CubeCounter)));

}

Thanks!