I hear this argument a LOT and hear that templates/generics are far more superior in code production than virtually-driven classes.

I still don't understand this. I've heard the virtual functions cause problems for performance and that the solution is always to use Templates/Generics.

Do they mean that it is better to have a Generic class that has a Template parameter of the base classes and runs individual methods of the same name vs Inherited members with virtual functions?

I still can't really picture this scenario, yet I know it's important to know the difference since many programmers seem to stress the importance of using Templates in place of Inheritance.

Could the other problem be that it's confusing for programmers to know (by heart) the extensions of classes and their separate functionalities? I was browsing Microsoft's .NET section in their MSDN site and noticed that they implemented new syntax just to code around Inheritance issues.

Recommended Answers

All 2 Replies

Whenever you hear people raging a lot about stuff like that you can almost always just ignore it.

The overhead of a virtual function call is so small that you have really got to be pushing the limits for it to make a difference.

Hmm... here's a link to the C++FAQ-Lite on virtual vs. static linkage:
http://www.parashift.com/c++-faq-lite/virtual-functions.html

Further, oo and generic programming are not orthogonal. They are simply different ways of handling different classes of problems. When the types of problem intersect, so do the methods to solve them.

For example, the STL I/O library uses both (polymorphic) inheritance and templates to provide us with such useful examples. But it isn't slow (when used properly).
http://www.artima.com/cppsource/scattered_io.html
(Most of the stuff you'll find where people are benchmarking I/O against C++ uses either outdated C++ libraries and/or stacks the test against C++ by using a lower class of I/O for comparison.)

So when people start expounding the virtues of <feature X> just nod your head and smile. Let them continue in their cultish delusions. And you can chuckle latter and feel perfectly justified using whichever technique best suits the problem.

Hope this helps.

> I've heard the virtual functions cause problems for performance and that the solution is always to use Templates/Generics.
Virtual functions can cause performance problems, but so can templates. The only real solution is to measure where the bottleneck is and deal with it specifically. Vague guidelines like "use templates instead of inheritance for performance" are a good way to produce horrible software.

> many programmers seem to stress the importance of using Templates in place of Inheritance
Edward hasn't met any of them. Inheritance is primarily polymorphism of behavior. Templates are polymorphism of data. Sometimes those two goals intersect, but a lot of the time they don't, and trying to fit a mismatched solution and problem together results in bad code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.