How to create a cube manually from c++ code?

Hey, everybody~ I’m complete novice at ue4.
and here’s my question confused me.
How to create a cube as a type of AActor or component from c++ code.
I want to create and keep it from the constructor.
Is there any snip of code could tips me? much appreciate for you!

C++ code is not really the best way to do this, since you are dealing with assets (the 3d asset for the mesh of the cube and the material) and that is usually cleaner in blueprints. But here is how you would approach it: Make a new class deriving from AActor, and attach a UStaticMeshComponent in the constructor as the root component of that actor:

UStaticMeshComponent* mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Cube Mesh"));
this->SetRootComponent(mesh);

and then assign the mesh and material to it (this is the part that is way cleaner in Blueprints, because you can just pick the assets from the details panel). You would pass your assets to mesh->SetStaticMesh(); and mesh->SetMaterial(); To check out how referencing assets in C++ works you can look here.

pulp_user
Thank you for your suggestion! The work what I did is much easy in blueprint indeed.
Your codes are exactly what I want. and they work well!!!
Thank you again for your answer!

Glad I could help! Please accept the answers so the question is marked as resolved.