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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:29:15+00:00 2026-05-27T09:29:15+00:00

If class inherits base class with static variable member, Will be their only one

  • 0

If class inherits base class with static variable member, Will be their only one member that shared with all classes that inheritances.

I have few kinds inherits classes, and many instance of every one of them.
I want that every one of the inherits classes will have a separate static member, that shared with all of its instances.

How can it be done?

thank you, and sorry about my poor English.

edit:

class a{
static int var;};
class b::a{};
class c::a{};

Now, I want that all instances of b will have same var, and all instances of c will have same var , but the var of b will be different from the var of c.

I’m sorry again about my English, if you can correct me, please do it.

  • 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-27T09:29:16+00:00Added an answer on May 27, 2026 at 9:29 am

    You can work aroud that using CRTP :

    struct YourBaseBaseClass {
        // put everything that does not depend on your static variable
    };
    
    template <YourSubclass>
    struct YourBaseClass : YourBaseBaseClass {
        static Member variable;
        // and everything that depend on that static variable.
    };
    
    struct AClass : YourBaseClass<AClass> {
         // there is a "variable" static variable here
    };
    
    struct AnotherClass : YourBaseClass<AnotherClass> {
         // which is different from the "variable" static variable here.
    };
    

    AClass and AnotherClass both have a variable static variable (of type Member), but the first one is a static variable from YourBaseClass<AClass> and the other is from YourBaseClass<AnotherClass>, which are two different classes.

    YourBaseBaseClass is optional, but you need it if you want to manipulate AClass and AnotherClass using a YourBaseBaseClass* pointer (you cannot have a YourBaseClass* pointer, because YourBaseClass is not a class).

    And remember to define those static variables.

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

Sidebar

Related Questions

I have simple base class with single static field. I have numerous classes that
Suppose I have a Base class and a Child class that inherits from Base
I have a class that inherits from a base class (this contains a lot
I have a class (Descendant1) that inherits from a base class (BaseClass). An instance
I'm trying to serialize a class that inherits from a base class that implements
I have a base class ActivityA that has some dialogs: public static final int
I have a library base class ( Controller ) and three sub-classes that inherit
I am trying to create a class in VB.NET which inherits a base abstract
I have my class X which inherits from Qt's class Base . I declared
The UserControl class inherits from TemplateControl which implements INamingContainer. Since this is only a

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.