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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:29:36+00:00 2026-06-18T00:29:36+00:00

I got a newbie C++ question, sorry about this… In C++ if I have

  • 0

I got a newbie C++ question, sorry about this…

In C++ if I have instance variables in a base class, all derived instances get their own personal copy of the base instance variable.

Now, if I have a class variable of the bass class (a static member), then all derived instances can share the base’ class variable.

I’d like to be able to create a base variable that is instantiated per instance of derived class but not by instances of derived classes of these derived classes.

Is this possible? How do I do so? Or what’s the best way (workaround) to do such a thing?

So to give a (bad) example:

Suppose I have class A and it has derived classes B, C, and D.
I also have class P and it has derived classes Q,R,S.

Now, I have a Configurator class. And it has many fancy functions that rely on instance variables of Configurator class. I like A and P to derive from a Configurator class, which stores a separate configurations for A and P. That way B,C,D have share a common configurations while Q,R,S share their own, separate, configuration.

But, Configurator class will create separate instances of configurations for B,C,D, and Q,R,S, because of the way it is written.

So, anyways, it would be nice to have Configurator class contain member variables that are instantiated by derived class instead of per instance.

thanks!

  • 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-18T00:29:37+00:00Added an answer on June 18, 2026 at 12:29 am

    The obvious way would be to use composition, not inheritance. This way, each of A and P have their own static configuration, shared among their respective derived classes.

    class A {
      public: static Configurator config;
    };
    class P {
      public: static Configurator config;
    };
    class B : public A {};
    class C : public A {};
    class Q : public P {};
    class R : public P {};
    

    A less obvious way is to use CRTP.

    template <class Derived> class Configurator {
      public: static int configValue;
    };
    class A : public Configurator<A> {};
    class B : public A {};
    class P : public COnfigurator<P> {};
    class Q : public P {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for this newbie question, just got starting. I want a simple program to
Sorry, newbie question I know, however I've currently got mySQL results only showing when
Sorry if the question is stupid but I'm newbie to MySQL and got stuck
Forgive me if this is a newbie question: I've got a menu set to
I'm inexperienced with MATLAB, so sorry for the newbie question: I've got a large
This is a newbie question... I got this project and I want to use
This is a newbie question: I have a pre-existing function that I would like
I am a newbie with Fluent NHibernate and have got a question that may
I got this big newbie question. When i try the following; the 'servlet' turns
Apologies if this is a newbie question, but I have been unable to resolve

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.