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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:35:08+00:00 2026-06-12T13:35:08+00:00

Possible Duplicate: Overriding static variables when subclassing I have a set of classes that

  • 0

Possible Duplicate:
Overriding static variables when subclassing

I have a set of classes that are all derived from a base class. Any of these derived classes declare the same static variable. It is however specific to each of the derived classes.

Consider the following code.

class Base {
    // TODO: somehow declare a "virtual" static variable here?
    bool foo(int y) { 
        return x > y; // error: ‘x’ was not declared in this scope
    }
};

class A : public Base {
    static int x;
};

class B : public Base {
    static int x;
};

class C : public Base {
    static int x;
};

int A::x = 1;
int B::x = 3;
int C::x = 5;

int main() {}

In my base class I wanted to implement some logic, that requires the knowledge of the derived-class-specific x. Any of the derived classes has this variable. Therefore I would like to be able to refer to this variable at base class scope.

This wouldn’t be a problem if it were a simple member variable. However, semantically, the variable is indeed not a property of the derived class’ instance, but rather of the derived class itself. Therefore it should be a static variable.

UPDATE I need the class hierarchy to preserve its polymorphic nature. That is, all my derived class’ instances need to be members of a common base class.

Then however, how can I get my hands on this variable from the base class method?

  • 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-12T13:35:11+00:00Added an answer on June 12, 2026 at 1:35 pm

    You can use the Curiously recurring template pattern.

    // This is the real base class, preserving the polymorphic structure
    class Base
    {
    };
    
    // This is an intermediate base class to define the static variable
    template<class Derived>
    class BaseX : public Base
    {
        // The example function in the original question
        bool foo(int y)
        { 
            return x > y;
        }
    
        inline static int x;
    };
    
    class Derived1 : public BaseX<Derived1>
    {
    };
    
    class Derived2 : public BaseX<Derived2>
    {
    };
    

    Now classes Derived1 and Derived2 will each have a static int x available via the intermediate base class! Also, Derived1 and Derived2 will both share common functionality via the absolute base class Base.
    Note: The variable declaration uses C++17’s inline static member declaration to avoid the need for a separate template variable definition.

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

Sidebar

Related Questions

Possible Duplicate: Overriding equals and hashCode in Java All, I have defined my class
Possible Duplicate: Is there a way to call an object's base class method that's
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Returning false from Equals methods without Overriding One of my colleague asked
Possible Duplicate: Why doesn't Java allow overriding of static methods? Is there any legitimate
Possible Duplicate: Overriding the power button in Android Is there any possible way I
Possible Duplicate: Overriding vs Virtual In C++, whether you choose to use virtual or
Possible Duplicate: C++ Overriding… overwriting? What's the difference between override and overwrite ? I've
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always

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.