开发者

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

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

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 开发者_Python百科of things like virtual functions?


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


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.


Templates are generally more performant at runtime, since more work is done at compile time. On the other hand, they can be more complex, thus difficult to write and understand. So, it's best to use them whenever you can and when it doesn't overcomplicate the whole solution.

For your examples, they are not exactly functionally equivalent: first variant allows and requires you to create instance of a parser at runtime, whereas second variant requires the parser to be chosen by the programmer when he writes code.


My suggestion is to use neither in this case and use separate functions for each file type;

    Stream stream = open(filepath);

    Image image = ParseBMP(stream);
    // ...or
    Image image = ParseJPG(stream);

No need to make things more complicated than they are.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜