Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6120547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:40:51+00:00 2026-05-23T15:40:51+00:00

In many situations, the question doesn’t even ask itself, since sometimes inheritance provides necessary

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:40:51+00:00Added an answer on May 23, 2026 at 3:40 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In many situations, JavaScript parsers will insert semicolons for you if you leave them
Possible Duplicate: Properties vs Methods For many situations it is obvious whether something should
I'm writing an app, and in many situations need to have direct access to
I have been in both situations: Creating too many custom Exceptions Using too many
Many of us need to deal with user input, search queries, and situations where
Although I found many answers and discussions about this question, I am unable to
I know this question has been asked so many times before but I couldn't
This will appear to be the age old question (which it is :)) that
I know this question was asked many times, but I didn't find my situation:
ive searched this question on google and i've found many answers but none of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.