0
Can we use Templates in Inheritance?
5 Réponses
+ 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.
+ 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.
0
But how can we make our code effective without templates?I mean they are the generic data types
0
Thanks bro
0
No prob ;-)