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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:20:13+00:00 2026-06-15T14:20:13+00:00

Possible Duplicate: Generating Structures dynamically at compile time I am now facing a situation

  • 0

Possible Duplicate:
Generating Structures dynamically at compile time

I am now facing a situation where I want a derived class to inherit from either Base1 or Base2 depending on a condition (in C++03). This means, I want to implement something like:

// pseudo-C++ code
class Derived : public
    if(condition) Base1    // inherit from Base1, if condition is true
    else Base2             // else inherit from Base2
{ /* */ };

This is likely not a good design, but the real world is not perfect.

I have searched here for an answer, but I do not want to use a preprocessor directive Problems with ifdef based inheritance in C++.

How else could I achieve this?

  • 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-06-15T14:20:13+00:00Added an answer on June 15, 2026 at 2:20 pm

    I figured out the solution using templates and partial specialization. The below code does the trick:

    // provide both the required types as template parameters
    template<bool condition, typename FirstType, typename SecondType>
    class License {};
    
    // then do a partial specialization to choose either of two types 
    template<typename FirstType, typename SecondType>
    class License<true, FirstType, SecondType> {
    public:    typedef FirstType TYPE;     // chosen when condition is true
    };
    
    template<typename FirstType, typename SecondType>
    class License<false, FirstType, SecondType> {
    public:    typedef SecondType TYPE;    // chosen when condition is false
    };
    
    class Standard {
    public:    string getLicense() { return "Standard"; }
    };
    
    class Premium {
    public:    string getLicense() { return "Premium"; }
    };
    
    const bool standard = true;
    const bool premium = false;
    
    // now choose the required base type in the first template parameter
    class User1 : public License<standard, Standard, Premium>::TYPE {};
    class User2 : public License<premium, Standard, Premium>::TYPE {};
    
    int main() {
        User1 u1;
        cout << u1.getLicense() << endl;   // calls Standard::getLicense();
        User2 u2;
        cout << u2.getLicense() << endl;   // calls Premium::getLicense();
    }
    

    The syntax looks unclean, but the result is cleaner than using preprocessor directive.

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

Sidebar

Related Questions

Possible Duplicate: Generating a JavaScript array from a PHP array I just want to
Possible Duplicate: passing variables from php to javascript I'm dynamically generating a list. I
Possible Duplicate: generating random numbers from skewed normal distribution i expect a long array
Possible Duplicate: Java: generating random number in a range I want to generate a
Possible Duplicate: Retrieve JIT output Generating Assembly from C# code? I started to learn
Possible Duplicate: Is there a best practice for generating html with javascript I want
Possible Duplicate: Generating random numbers in Javascript Hi.. I want to generate random numbers
Possible Duplicate: XJC Generating Integer Instead of int Need to generate entity class with
Possible Duplicate: Generating excel documents programmatically I want to generate excel reports using Java/J2EE.
Possible Duplicate: Generating random integer from a range I just started learning C++ and

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.