-
May 26th, 2010, 08:01 AM
#1
Class Template Inheritance
I have seen many class template that inherits from a normal base class.
What is the advantages about this design ?
Thanks.
Thanks for your help.
-
May 26th, 2010, 08:12 AM
#2
Re: Class Template Inheritance
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
May 27th, 2010, 01:03 AM
#3
Re: Class Template Inheritance
Thanks for your reply.
For one, you can store objects with different template parameters polymorphically, e.g. in a std::vector<Base*>.
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?
Second, there may also be some common functionality or a common interface that can be accessed through the base class.
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 ?
Thanks.
Thanks.
Thanks for your help.
-
May 27th, 2010, 02:25 AM
#4
Re: Class Template Inheritance
Originally Posted by Peter_APIIT
I have seen many class template that inherits from a normal base class.
What is the advantages about this design ?
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).
Last edited by nuzzle; May 27th, 2010 at 02:52 AM.
-
May 27th, 2010, 02:52 AM
#5
Re: Class Template Inheritance
Originally Posted by Peter_APIIT
I have seen many class template that inherits from a normal base class.
What is the advantages about this 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.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
May 27th, 2010, 04:59 AM
#6
Re: Class Template Inheritance
Originally Posted by Peter_APIIT
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?
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>).
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 ?
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.
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.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
May 27th, 2010, 06:28 AM
#7
Re: Class Template Inheritance
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
May 27th, 2010, 09:00 PM
#8
Re: Class Template Inheritance
Thanks for the reply.
What is myClass_helpe ?
Thanks for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|