Add components in C++

Trying to using a TArray and a TArray
to add components dynamicly.
This is my plan:
be able to add a skeletalmesh at runtime or blueprint, and use a TArray to hold all SkeletalMeshComponent created by skeletalmeshes.
I already created two TArrays, but don’t know how to make my skeletal meshes into components.
Anyone help me?

You can do something like this:

USkeletalMeshComponent* mesh = NewObject<USkeletalMeshComponent>(TheActor,"skeletal mesh");
mesh->SetupAttachment(TheActor->GetRootComponent());
mesh->RegisterComponent();

Where TheActor would be the actor, that you want the skeletal mesh to attach to. You could then add them to your array like this:

TArray<USkeletalMeshComponent*> YourArray;
YourArray.Add(mesh);

Also, do not forget to

mesh->SetSkeletalMesh(YourSkeletalMesh);

In UE, a SkeletalMesh (asset) does not automatically create a SkeletalMeshComponent (component) for it: you have to create it like pulp_user explained, and then assign the SkeletalMesh to it.

Thank you guys.
One more question, is it alright to use the same object(in your reply, “mesh”), to NewObject and add it to the array? like this:
for(…)
{
mesh = newobject<…>(…);
ComponentsArray.add(mesh);
}
will it work?

It works fine, thank you guys!