Can we use Templates in Inheritance? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can we use Templates in Inheritance?

9th Jul 2016, 5:30 PM
Desik
Desik - avatar
5 Answers
+ 1
Yes you can. This can be a cause auf confusion as templates can be considered to do some sort of static polymorphism (polymorphism at compile time) and classical OOP also provides a way to do static polymorphism but also dynamic polymorphism (polymorphism at runtime). There are techniques dealing with templates that express type hierarchies as "Curiously Recurring Template Pattern" (CRTP, https://en.m.wikipedia.org/wiki/Curiously_recurring_template_pattern) that add to the confusion. In addition, from my personal experience, there are compilers that have problems with it (e.g. Visual Studio 2013: internal compiler error). In other words: pls make sure you understand templates and OOP, their similarities and differences deeply before you start mixing these two programming techniques. Also, if you work for a company, pls be aware that code that uses templates and OOP in an intertwined way, will be hard to understand for your colleagues. If there's a more simple solution to model your program, consider using that.
9th Jul 2016, 5:51 PM
Stefan
Stefan - avatar
+ 1
Well, without templates you pay a penalty of less than 10% runtime overhead for the call of a virtual function. Using type-specific code inside the function might yield more than 10% gain. Nevertheless, 10% slower runtime is usually acceptable in terms of overall cost in comparison to far more than just 10% more time and therefore cost in coding for the same function.
9th Jul 2016, 6:44 PM
Stefan
Stefan - avatar
0
But how can we make our code effective without templates?I mean they are the generic data types
9th Jul 2016, 5:54 PM
Desik
Desik - avatar
0
Thanks bro
9th Jul 2016, 6:47 PM
Desik
Desik - avatar
0
No prob ;-)
9th Jul 2016, 11:35 PM
Stefan
Stefan - avatar