I have seen many class template that inherits from a normal base class.
What is the advantages about this design ?
Thanks.
Printable View
I have seen many class template that inherits from a normal base class.
What is the advantages about this design ?
Thanks.
For one, you can store objects with different template parameters polymorphically, e.g. in a std::vector<Base*>.
Second, there may also be some common functionality or a common interface that can be accessed through the base class.
Thanks for your reply.
If you have one class template that derived from a one base class and there are no other derived classes. Then, this a overhead to declare the derived class as class template. How you store them polymorphically?Quote:
For one, you can store objects with different template parameters polymorphically, e.g. in a std::vector<Base*>.
Bu you also can declare them in class template instead of the base class. What is the design usage for this class hierarchy ?Quote:
Second, there may also be some common functionality or a common interface that can be accessed through the base class.
I wonder whether this design related to CRTP ?
Thanks.
Thanks.
It's just a combination of the advantages that inheritance and generics offer separately.
The CRTP offers more. Here generics resolves a specific issue with inheritance used in isolation, namely that a base class shouldn't know its derived classes individually (because it increases coupling in a design).
I used such a design for my image classes. The base class contains all of the functionality that is common to all image types, but contains no information about any specific type.
A template derives from this which defines a specific type of image from the template parameters (bit depth, number of planes).
The non-template base class allows me to access the common functionality polymorphically or store references or pointers to images without having to know the specific type.
If you have a template class MyClass<T>, then you cannot store an instance of MyClass<int> and MyClass<float> in the same container. If you derive MyClass<T> from MyBase, then you can store objects of MyClass<T> allocated with new in a container of MyBase* (or std::unique_ptr<MyBase>).
If you declare no functionality in the base class, then you can only use it to store objects with different template arguments. If you want to do something with those objects, you will have to know the full type. However, if you add (virtual) functions to my base class, you can call these functions polymorphically.Quote:
Bu you also can declare them in class template instead of the base class. What is the design usage for this class hierarchy ?
I wonder whether this design related to CRTP ?
CRTP is really a different concept, because there you do not have a common base class. Instead each type derives from its own base class.
There are also issues of executable bloat.
Always remember that when you have a myClass<T>, you are basically re-compiling myClass (and generating the associated binary code) for every T you use it with. If you have a bunch of code that is independent from T, then there is no point in compiling it for each T, is there?
By doing the construct myClass<T> : public myBase, you avoid re-compiling myBase for every T out there. The goal here though has very little to do with polymorphism or OO, it's just an optimization of combining code independent of T in a class independent of T.
There are other ways of doing this, namely the friend "myClass_helper" class comes to mind.
Thanks for the reply.
What is myClass_helpe ?