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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:18:28+00:00 2026-06-18T11:18:28+00:00

Say I have a base class with a flag inside of it which derived

  • 0

Say I have a base class with a flag inside of it which derived classes have to set:

struct Base
{
    bool flag;
    Base(bool flag):flag(flag) {}
};

I want to configure which derived classes set the flag to true/false in a data-driven way – i.e. I’d like to configure this from a header.

struct Derived1 : Base
{
    Derived1() : Base( expr ) {}
};

Where expr is something (don’t know what yet) that is able to get the info from the header – tell whether Derived1 should make flag true or false. Ideally, I’d get an error if I make a new derived class but fail to specify the flag in the header, but this isn’t mandatory. This way I can just modify a single central location to make changes.

What’s the idiomatic approach for 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-18T11:18:29+00:00Added an answer on June 18, 2026 at 11:18 am

    An alternative version that uses a single function might be more compact:

    struct Derived1 : Base
    {
        Derived1() : Base(theFlag(this)) {}
    };
    

    Then in the header:

    template <typename T>
    bool theFlag(T*)
    {
       if (typeid(T) == typeid(Derived1)) return true;
       if (typeid(T) == typeid(Derived2)) return false;
       if (typeid(T) == typeid(Derived3)) return true;
    
       throw std::runtime_error("No theFlag is given for this type");
    }
    

    If you are married to the compile-time check, the best you could do is to introduce a bit of duplication:

    template <typename T>
    bool theFlag(T*)
    {
       static_assert(
          std::is_same<T, Derived1>::value ||
          std::is_same<T, Derived2>::value ||
          std::is_same<T, Derived3>::value,
          "No theFlag is given for this type"
       );
    
       if (typeid(T) == typeid(Derived1)) return true;
       if (typeid(T) == typeid(Derived2)) return false;
       if (typeid(T) == typeid(Derived3)) return true;
    }
    

    This basically relies on SFINAE – the compiler would not be able to find an overload for theFlag if you called it with an unsupported argument, essentially.

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

Sidebar

Related Questions

Let say we have a base class and its two derived classes; The base
Say I have several derived classes whose base class is a generic class. Each
Let's say I have a Base class and several Derived classes. Is there any
Let's say I have a base class 'A' and two classes 'B' and 'C',
Let's say we have these two classes: public class Base { public static int
For example: I have an implementation (say doSomething()) in Base class. I want all
Let's say I have a base class Animal from which a class Cow inherits,
I have a base class, say BassClass , with some fields, which I made
let's say I have a pointer to some base class and I want to
Say I have the following classes: template <class T> class Base { protected: T

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.