Yesterday, I was asked a question, what are the performance trade offs between using inheritance and templates?
I said in case of virtual functions, the dynamic linkage will cause some performance problem when it comes to inheritance. However, with templates, the specialized classes will be generated during compile time itself, so there is no performance problem except the size of the executable grows along with the number of specialized classes.
Can anyone explain if there are other things to consider here?
Thx!
Rahul.
first of all – inheritance & templates are – in my point of view – orthogonal to each other (see OOSC for a chapter on that). In C++ the trade-offs basically boil down to:
requires code to be generated.
So it’s space vs. speed. But you can have the benefits & drawbacks of both in the same class (see my first sentence – the features are orthogonal in what they want to reach).