Template Classes Inheritance


Template Classes & Inheritance

When studying template classes, the question might arise as to whether template classes may be used with inheritance. When consulting the books on the subject of template classes that I had in my library, I could find no examples of this topic discussed. But I still had the question in my mind, so I began to experiment on my compiler. Since I had no statement that this was a part of Standard C++, I did not know whether these examples were part of Standard C++ or a characteristic of my compiler. Since my original experiments, I have acquired a new compiler (Visual Studio.NET 2005) and updated the examples with only minor modifications and they still work.

The first possibility that came to mind was the case where a template class would be an extension of a class that is not a template class. See temp_ext.cpp. As you can see from this example, this is possible on Visual Studio .NET. The question remains is this possible on other compilers. Try it on other compilers because it may not be possible on that compiler.

The next question that I had was whether a template class can itself be an extension of a class that is also a template class but of the same unspecified type. See temp_ext2.cpp. As you can see from this example, this is possible on Visual Studio .NET. The question remains: Is this possible on other compilers? Try it on other compilers. (Note: Observe that in the base class the word class was used while in the derived class the keyword typename was used. This should make no difference if they are both the same.)

Another question would be: Can a non template class be derived from a template class? When I tried this, I could not get it to work. The reason is that the base class required an unknown data type for its attributes and this unknown data type would have to be carried over to the derived class as well. Therefore the derived class would have to include an unknown data type within its definitions. Meaning that it would have to be a template class. Further when an object of the derived class is defined, a data type would need to be specified in the definition of the object so that data could be placed within the memory of the object that pertained to the attributes of the template base class.

Next I wondered whether the two classes (i.e. the base and the derived class) can be of different class types. See temp_ext3.cpp. When I tried it in my version of the one compiler, it did not compile. You should try it on your compiler. This seemed a logical extension of one template class to another but this method did not work. However when I varied this just a bit and placed the type T of the base class in the header of the derived class, this example worked. See temp_ext3b.cpp.

Another interesting question that occurred to one of my former students was what about multiple inheritances. For a twist on multiple inheritance and template classes see temp_ext6c.cpp. As the student said who submitted the question, this example seems a little too complex and may be too difficult to maintain.

But what about a serial extension that contains more than two classes? See temp_ext4.cpp. In this example I tried a template class B that was a derived class of a template class A that was a derived class of a non template class C. As you can see from this example, this is possible on Visual Studio .NET. The question remains is this possible on other compilers. Try it on other compilers.

So template classes when used with serial inheritance works on some compilers. But what about multiple inheritance? See temp_ext5.cpp. In this example, I tried base template classes A and C and a template class B that was an extension of these two classes. As you can see from this example, this is possible. The question remains is this possible on other compilers?

But what about the case where the two base classes A and C where themselves template classes of the same type? See temp_ext6.cpp. As you can see from this example, this is possible on some compilers. The question remains is this possible on other compilers. Try it on your compiler.

As with the case of serial inheritance, it does not appear possible that inheritance from template classes of different types works with multiple inheritance either on some compilers unless the derived class has multiple typenames in its header to agree with the base template classes. The question then remains is this possible on other compilers? Try it on other compilers. To do this, change the previous example so that both A and C are template classes of different types.




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net