Saturday 23 June 2012

Why were the templates introduced?
When is a template a better solution than a base class?

Inheritance provides specialization in the run-time.
Templates provide specialization in the compile-time.

Containers and algorithms often operate over the various types in the same way and templates allow separating their generic code from the type-dependent code.

Templates allow writing generic (type-independent) code for containers and algorithms. Their specializations (for a given types - template parameters) are created during compile-time.

Virtual function achieves dynamic polymorphism at runtime while template achieves static polymorphism at compile time.

Templates => compile time polymorphism
Abstract classes => run-time polymorphism

No comments:

Post a Comment