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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:07:48+00:00 2026-06-01T14:07:48+00:00

In my project, there is one Base class and there are 3 different derived

  • 0

In my project, there is one Base class and there are 3 different derived classes. I have some macro variables written as const int in the base class. All the derived member is going to access these members and are going to see the same value. Since, its a const, and it is not going to change, maintaining 3 different values will be waste of space. Hence I want a way to maintain just one copy of base class const members so that all the derived class will use the same.

Example

 //Base.hpp
 Class Base {

       const int ZER0;
       const int ONE;
 };

 //Derived1.hpp
 class Derived1:public Base {
      int some_method1(); 
 };

  //Derived1.cpp

  int Derived1::some_method1() {

    int value = ZERO;
  }

 //Derived2.hpp

 class Derived2:public Base {
    int some_method2();
 };

//Derived2.cpp

int Derived2::some_method2() {
   int value = ONE;
}

//main.cpp

Derived1 d1;
d1->some_method1();

Derived d2;
d2->some_method2();

//Here in both the methods the values are constant, but still the ZERO and ONE are going to have different space. Is there a way, where I maintain only one copy? I could think of static variable. Is it okay to use static inside class and where do I initialize the static variable. Please advice.

PS: This questions might be a duplicate, but I cant find something with similar words, if you feel this is, please feel free to redirect to the corresponding article/question;

  • 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-01T14:07:49+00:00Added an answer on June 1, 2026 at 2:07 pm

    If I’m understanding you correctly, would an enum do what you’re looking for?

    class Base
    {
        enum 
        {
            ZERO,
            ONE
        };
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one java project in netBeans. There are some packages, and classes in
OK, so I have two (completely unrelated, different project) classes using iterators now. One
I have a mvc project and on one page there is a table of
In my project I have a few functions in derived classes that are the
I have a project with a base view controller class, that is inherited by
I'm working on problem 9 in Project Euler: There exists exactly one Pythagorean triplet
I am working on one project where there is a functionality need to implement
I'm currently estimating a new project. My high level estimate assuming there was one
I'm working with the very-useful ID3 framework in my cocoa project. There's one tiny
In virtually every project I've ever worked on, there will be one or two

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.