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

  • SEARCH
  • Home
  • 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 141073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:43:04+00:00 2026-05-11T07:43:04+00:00

What is the relationship between using virtual functions and C++ inheritance mechanisms versus using

  • 0

What is the relationship between using virtual functions and C++ inheritance mechanisms versus using templates and something like boost concepts?

It seems like there is quite an overlap of what is possible. Namely, it appears to be possible to achieve polymorphic behavior with either approach. So, when does it make sense to favor one over the other?

The reason why I bring this up is because I have a templated container, where the containers themselves have a hierarchical relationship. I would like to write algorithms that use these containers without caring about which specific container it is. Also, some algorithms would benefit from knowing that the template type satisfied certain concepts (Comparable, for example).

So, on one hand, I want containers to behave polymorphicly. On the other, I still have to use concepts if I want to correctly implement some algorithms. What is a junior developer to do?

  • 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. 2026-05-11T07:43:05+00:00Added an answer on May 11, 2026 at 7:43 am

    I think of concepts as a kind of meta-interface. They categorize types after their abilities. The next C++ version supplies native concepts. I hadn’t understood it until i came across C++1x’s concepts and how they allow putting different yet unrelated types together. Imagine you have a Range interface. You can model that with two ways. One is a subtype relationship:

    class Range {     virtual Iterator * begin() = 0;     virtual Iterator * end() = 0;      virtual size_t size() = 0; }; 

    Of course, every class that derives from that implements the Range interface and can be used with your functions. But now you see it is limited. What about an array? It’s a range too!

    T t[N];  begin() => t end() => t + size() size() => N 

    Sadly, you cannot derive an array from that Range class implementing that interface. You need an extra method (overloading). And what about third party containers? A user of your library might want to use their containers together with your functions. But he can’t change the definition of their containers. Here, concepts come into game:

    auto concept Range<typename T> {     typename iterator;     iterator T::begin();     iterator T::end();     size_t T::size(); } 

    Now, you say something about the supported operations of some type which can be fulfilled if T has the appropriate member functions. In your library, you would write the function generic. This allows you accept any type so long as it supports the required operations:

    template<Range R> void assign(R const& r) {     ... iterate from r.begin() to r.end().  } 

    It’s a great kind of substitutability. Any type will fit the bill that adheres to the concept, and not only those types that actively implement some interface. The next C++ Standard goes further: It defines a Container concept that will be fit by plain arrays (by something caled concept map that defines how some type fits some concept) and other, existing standard containers.

    The reason why I bring this up is because I have a templated container, where the containers themselves have a hierarchical relationship. I would like to write algorithms that use these containers without caring about which specific container it is. Also, some algorithms would benefit from knowing that the template type satisfied certain concepts (Comparable, for example).

    You can actually do both with templates. You can keep having your hierarchical relationship to share code, and then write the algorithms in a generic fashion. For example, to communicate that your container is comparable. That’s like standard random-access/forward/output/input iterator categories are implemented:

    // tag types for the comparator cagetory struct not_comparable { }; struct basic_comparable : not_comparable { };  template<typename T> class MyVector : public BasicContainer<T> {     typedef basic_comparable comparator_kind; };  /* Container concept */ T::comparator_kind: comparator category 

    It’s a reasonable simple way to do it, actually. Now you can call a function and it will forward to the correct implementation.

    template<typename Container> void takesAdvantage(Container const& c) {     takesAdvantageOfCompare(c, typename Container::comparator_kind()); }  // implementation for basic_comparable containers template<typename Container> void takesAdvantage(Container const& c, basic_comparable) {     ... }  // implementation for not_comparable containers template<typename Container> void takesAdvantage(Container const& c, not_comparable) {     ... } 

    There are actually different techniques that can be used to implement that. Another way is to use boost::enable_if to enable or disable different implementations each time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I downloaded Python 2.5.1 and compiled it from the source.… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer You can use something like this: class B { private… May 11, 2026 at 11:30 pm
  • Editorial Team
    Editorial Team added an answer Turns out this was my own fault. There is nothing… May 11, 2026 at 11:30 pm

Related Questions

How do you map a class to other instances of the same class when
In a software system a one-to-one relationship must be modeled between two objects where
The software I'm working with has 2 tables, lead and customer. When we sell
I currently use Notepad++ for most of my development. I have been checking out

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.