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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:17:18+00:00 2026-05-18T02:17:18+00:00

Let me explain what I am asking for by an example. Imagine I have

  • 0

Let me explain what I am asking for by an example. Imagine I have a class for a car.
Now, the car may have a lot of extras:

  • 4 doors instead of only 2
  • Automatic door locking
  • 4 Wheel drive

I want to create the class with any combination of these options. Any of these options needs some data members. Imagine the class now looks like this:

class Car {
public:
  bool FourDoors;
  bool AutomaticDoorLocking;
  bool FourWheelDrive;

  Door doors[4];  //4 only needed if FourDoors=true
  DoorLockingElectronic doorElectronic; //Only needed if AutomaticDoorLocking=true
  TransmissionsShafts[4]; //4 only needed for FourWheelDrive=true

  void lockDoors() {
    if (AutomaticDoorLocking) {
      doorElectronic.lockDoors();
    } else {
      // Do manual door locking
    }
  }
};

So far so good, but now I want to create a lot of cars, so many that memory gets critical. And I do not need most of the extras in most of those cars.
I could create a base class, and derive classes with those options enabled or disabled.
But I would have to create 2^{#extras} classes to create all possible combinations, with a lot of double code.

So I thought maybe templates could be used? (that is the question).

I can imagine having a flag template, and rewrite the lockDoors like this:

template<int flags>
void Car<flags>::lockDoors() {
  if (flags | AutomicDoorLockingFlag) {
    doorElectronic.lockDoors();
  } else {
    // Do manual door locking
  }
}

Wonderful! But the class Car<0> still takes a lot of unnecessary space. So:

Can I somehow include or exclude class members depending on a template parameter?

Other Ideas how to deal with the situation are also welcome!

  • 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-18T02:17:18+00:00Added an answer on May 18, 2026 at 2:17 am

    You want to use policy classes:

    class FourDoorPolicy { Door m_doors[4]; ... };
    class TwoDoorPolicy { Door m_doors[2]; ... };
    
    class AutoDoorLockingPolicy { ... };
    class ManualDoorLockingPolicy { void lockDoors(); ... };
    
    class FourWheelDrivePolicy { TransmissionShafts m_shafts[4]; ... };
    class TwoWheelDrivePolicy { TransmissionShafts m_shafts[2]; ... };
    
    template <class DoorPolicy = TwoDoorPolicy,
              class LockingPolicy = ManualDoorLockingPolicy,
              class DrivePolicy = TwoWheelDrivePolicy>
    class Car : public DoorPolicy, public LockingPolicy, public DrivePolicy
    {
      ...
    };
    

    Put all the policy specific stuff (e.g. lockDoors() function) inside the policy classes rather than the Car class. The Car class inherits these, which is a form of composition (i.e. you are building all their functionality into the Car class).

    Note that you should give all the policy classes a protected, non-virtual destructor so that they can only be instantiated as part of a derived class.

    You then instantiate customised cars in the normal template manner:

    Car<FourDoorPolicy, AutoDoorLockingPolicy, TwoWheelDrivePolicy> myCar;
    

    Of course, you can use typedefs to help with this (and template aliases in C++0x will help a lot, too).

    See: Policy-based Design

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

Sidebar

Related Questions

To start things off, let me explain that I'm working with a class that
To start things off, let me explain that I'm working with a class that
Let's explain the context: I have a person form inside a jquery dialog that
the title could be somewhat misleading so let me explain what I'm trying to
Let me explain my problem together with the background, so it would be easier
class TestClass { private string _privateString = hello; void ChangeData() { TestClass otherTestClass =
I have the following code (OLE = OptimisticLockException) ... public void outer() { try
This is currently my MySQL UPDATE query, which is called from program written in
The question is in the title... I searched but couldn't find anything. Edit: I

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.