So coming to the question. While using static polymorphism are we giving up the ability to control every derived class without knowing what class they are? Or is there a way to this?
Unless I'm missing something, your "WHAT HERE" comments are a problem. You no longer have a common base class. Because Animal is a template class, Cat derives from Animal<Cat> and Dog derives from Animal<Dog>. There is no such thing as an Animal, so you cannot have common pointers to a base class.
@Duoas, i think, only after writing an example of static polymorphism i realized that such a thing was not possible. Which shocked me. It is that ability which amazes me about dynamic polymorphism. Thank you for the link by the way.
So i realized that it actually is a trade off. You choose which one better suits your needs.
Even if we do so, cant use it for that purpose. For example:
1 2 3 4 5 6 7 8 9 10
std::vector<std::unique_ptr<NonTemplateBase>> animals;
animals.push_back(std::unique_ptr<Cat>(new Cat()));
animals.push_back(std::unique_ptr<Dog>(new Dog()));
for(std::unique_ptr<NonTemplateBase> &animal : animals)
{
animal->talk(); // error: It is no longer Animal class so no talk() function
// Casting it is also impossible
}
Even if we do so, cant use it for that purpose. For example:
sure, just put a pure virtual method named talk in the non-template-base class.
Still, as mentioned before this involves dynamic polymorphism so you don't get any advantages.
Still, as mentioned before this involves dynamic polymorphism so you don't get any advantages.
Wrong. You may not get the advantages of static polymorphism, but you do get the advantages of dynamic polymorphism.
The undertone of this thread, explicit or not, intended or not, is that static polymorphism is better than dynamic polymorphism. That's like saying a screwdriver is better than a hammer. You can use a screwdriver to drive a nail, but a hammer is much more suited to the job. There are many times when dynamic polymorphism is exactly what is needed, and trying to use static polymorphism just won't get the job done.
Wrong. You may not get the advantages of static polymorphism, but you do get the advantages of dynamic polymorphism.
I meant to say "so you don't get any advantages by using static polymorphism in this case" but I don't know what else I could've wanted to say that you needed to "correct" me
still, i think this thread is done, I hope we could help you Ceset :)