Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use templates instead of inheritance, and vice versa? [closed]

In many situations, the question doesn't even ask itself, since sometimes inheritance provides necessary features which templates can't provide. For example, when I need to address different types via one base-type (polymorphism), I need to use inheritance.

However, there are some instances where the problem can be solved both with inheritance, as well as templates.

Take for example strategy pattern-like parametrization of certain parts of the code:

One solution of a file-parser could look like this:

class FileParser
{
   //...
   public:
      void Parse(ParsingAlgorithm* p);
   //...
}

void FileParser::Parse(ParsingAlgorithm* p)
{
   m_whatevertypeofvaluesineed = p->Parse(whateverparametersyouneed);
}

where ParsingAlgorithm is an abstract base class, which provides some basic methods and needs to be inherited by whoever likes to implement a specific parser for the FileParser class.

However, the same can easily be achieved using templates:

template <class Parser>
class FileParser
{
   //...
   public:
      void Parse()
      {
           m_whatevertypeofvaluesineed = m_parser.Parse(whateverparametersyouneed);
      }

   private:
      Parser m_parser;
   //...
}

Are there some general rules that I can use to decide whether to use templates or inheritance? Or should I simply use templates wherever possible, in order to avoid run-time overhead of things like virtual functions?

like image 894
TravisG Avatar asked Jul 12 '11 11:07

TravisG


People also ask

When should I use template classes?

Templates can be used in conjunction with abstract datatypes in order to allow them to handle any type of data. For example, you could make a templated stack class that can handle a stack of any datatype, rather than having to create a stack class for every different datatype for which you want the stack to function.

When should templates be used C++?

Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. To simply put, you can create a single function or single class to work with different data types using templates. C++ template is also known as generic functions or classes which is a very powerful feature in C++.


2 Answers

If you know during compile-time what objects you're going to manipulate, then static polymorphism with templates is often the fastest way to go, and it produces code that's a little bit more concise (no need for explicit inheritance). It can also be more generic as you're not restricted to a strong class hierarchy.

If you want run-time polymorphism, then you have no choice but to use pointers, inheritance and the slight overhead of virtual functions.

My own opinion:

  • Use templates when possible, it's comfortable
  • Use inheritance to factorise code (but not to have heterogenous collections), but be careful with slicing.
  • Don't worry about the performance issues of virtual calls
  • Sometimes you have no choice and you want hetergenous collections dragging around the pain of using pointers
like image 58
Tristram Gräbener Avatar answered Nov 15 '22 18:11

Tristram Gräbener


Templates provide compile-time polymorphism as opposed to run-time polymorphism provided by inheritance. I prefer using templates when I can.

This article on Templates and Inheritance explains it in detail.

like image 25
Ozair Kafray Avatar answered Nov 15 '22 18:11

Ozair Kafray