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 136359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:56:09+00:00 2026-05-11T06:56:09+00:00

If I want to make a class adaptable, and make it possible to select

  • 0

If I want to make a class adaptable, and make it possible to select different algorithms from the outside — what is the best implementation in C++?

I see mainly two possibilities:

  • Use an abstract base class and pass concrete object in
  • Use a template

Here is a little example, implemented in the various versions:

Version 1: Abstract base class

class Brake { public: virtual void stopCar() = 0;   };  class BrakeWithABS : public Brake { public: void stopCar() { ... } };  class Car {   Brake* _brake; public:   Car(Brake* brake) : _brake(brake) { brake->stopCar(); } }; 

Version 2a: Template

template<class Brake> class Car {   Brake brake; public:   Car(){ brake.stopCar(); } }; 

Version 2b: Template and private inheritance

template<class Brake> class Car : private Brake {   using Brake::stopCar; public:   Car(){ stopCar(); } }; 

Coming from Java, I am naturally inclined to always use version 1, but the templates versions seem to be preferred often, e.g. in STL code? If that’s true, is it just because of memory efficiency etc (no inheritance, no virtual function calls)?

I realize there is not a big difference between version 2a and 2b, see C++ FAQ.

Can you comment on these possibilities?

  • 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-11T06:56:09+00:00Added an answer on May 11, 2026 at 6:56 am

    This depends on your goals. You can use version 1 if you

    • Intend to replace brakes of a car (at runtime)
    • Intend to pass Car around to non-template functions

    I would generally prefer version 1 using the runtime polymorphism, because it is still flexible and allows you to have the Car still have the same type: Car<Opel> is another type than Car<Nissan>. If your goals are great performance while using the brakes frequently, i recommend you to use the templated approach. By the way, this is called policy based design. You provide a brake policy. Example because you said you programmed in Java, possibly you are not yet too experienced with C++. One way of doing it:

    template<typename Accelerator, typename Brakes> class Car {     Accelerator accelerator;     Brakes brakes;  public:     void brake() {         brakes.brake();     } } 

    If you have lots of policies you can group them together into their own struct, and pass that one, for example as a SpeedConfiguration collecting Accelerator, Brakes and some more. In my projects i try to keep a good deal of code template-free, allowing them to be compiled once into their own object files, without needing their code in headers, but still allowing polymorphism (via virtual functions). For example, you might want to keep common data and functions that non-template code will probably call on many occasions in a base-class:

    class VehicleBase { protected:     std::string model;     std::string manufacturer;     // ...  public:     ~VehicleBase() { }     virtual bool checkHealth() = 0; };   template<typename Accelerator, typename Breaks> class Car : public VehicleBase {     Accelerator accelerator;     Breaks breaks;     // ...      virtual bool checkHealth() { ... } }; 

    Incidentally, that is also the approach that C++ streams use: std::ios_base contains flags and stuff that do not depend on the char type or traits like openmode, format flags and stuff, while std::basic_ios then is a class template that inherits it. This also reduces code bloat by sharing the code that is common to all instantiations of a class template.

    Private Inheritance?

    Private inheritance should be avoided in general. It is only very rarely useful and containment is a better idea in most cases. Common case where the opposite is true when size is really crucial (policy based string class, for example): Empty Base Class Optimization can apply when deriving from an empty policy class (just containing functions).

    Read Uses and abuses of Inheritance by Herb Sutter.

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

Sidebar

Ask A Question

Stats

  • Questions 96k
  • Answers 96k
  • 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 Even if your solution is not fort-knox, you should be… May 11, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer Solved the problem, with a big thanks to http://poocs.net/2007/11/14/special-characters-and-nested-routes (and… May 11, 2026 at 7:17 pm
  • Editorial Team
    Editorial Team added an answer I'm not an ASP programmer, but I do a heck… May 11, 2026 at 7:17 pm

Related Questions

If i want to extend a class like AVAudioPlayer, whats the best way to
I'm a freshman in college going for my computer science degree... I've programmed plenty
I'm looking for patterns that concern coding parts of a GUI. Not as global
I'm trying to implement a log viewer in my application. The idea is simple,

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.