Hello, I have tried to make my start of a Component Manager for all bunch of Components I want in my project.
I have almost never used Polymorphism so I want your thoughts about it.
Please Tell me if there's something wrong or if I should do it in an other way (I did try to make the Component Manager a variadic templated class, but I could not have managed to make it work), Thanks.
(NOTE: This is not my final version of this code, that's also why I did not do the Big Five Rule)
I have almost never used Polymorphism so I want your thoughts about it.
I think runtime polymorphism is rarely useful. But if you do expect to make the decision, at runtime, such as based on some user input, whether an Another or an Is would be placed in the same Container_Manager, then this is indeed one of several possible implementation techniques.
cp->insert_component(std::move(new Is()));
that move does nothing: there's no rvalue overload of insert_component, and even if there was one, moving from a pointer (which is what new returns) is the same thing as copying a pointer.
More importantly, you shouldn't have a reason to use the keyword new in C++, especially in code that advertises itself to be C++14-only by using auto main -> int. Make your main function more like
1 2 3 4 5 6 7
int main()
{
Component_Manager<5> cp;
cp.insert_component(std::make_unique<Is>());
auto another = std::make_unique<Another>();
cp.insert_at(std::move(another), 3);
}
Whenever you are tempted to give something the name "Manager" ask yourself "what am I doing wrong here?" Why do you need an object manager? Is the C++ runtime not a good enough object manager for you?
In general, you want a factory or a pool or a container. You almost never want a manager.
My first recommendation is to write comments explaining why a class exists -- it's single responsibility. Managers rarely have single responsibilities and therefore violate the single responsibility principal.
Good class comments describe why a type exists (its single responsibility), how to use it properly (including limitations, lifecycle, thread safety, etc) and its invariants. I always start with comments, that way I know what I am doing and, when I seek help from others, they know my intentions.
In your case, your component manager is merely a container. Why not just use a standard container?