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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:30:31+00:00 2026-06-12T04:30:31+00:00

i do have a question about static members inheritance and them protection in C++.

  • 0

i do have a question about static members inheritance and them protection in C++. I hope i will be clear enough since is not always easy to write the mind states 🙂 I’m writing an simple (textual parser) for an graphical program that is loading textual file with custom formatting, now the text part is almost done and now i need to produce some objects to feed them with data i loaded from the file.

This question i believe belong to hour 1. of C++ but im stuck. For example i loaded from the text file just 2 types of logical “nodes”, LAYER and PLINE, they also have they attributes which can or cannot be common to both. The relation of LAYER to PLINE and back is totally irrelevant now, the thing that is bugging me is how to connect and handle attributes of both:

Suppose i chose DataObj as the base class for both. DataObj has an member called “name” because both LAYER and PLINE can have a name. LAYER has an attribute that is common only to layer for eg. “locked”, and PLINE has an attribute that is common only to pline eg. “color”. In the “school way” of doing things it would look like:

/// i use const char* for everything to not complicate things ... 
...
class DataObj {
  ...
  const char* name;
  ...
}
...
class Layer : public DataObj {
 ...
 const char* locked;
 ...
}
...
class Pline : public DataObj {
 ...
 const char* color;
 ... 
}
...
int main(){
   Layer* l = new Layer();
   l.name = "first layer";
   l.locked = "false";

   Pline* p = new Pline();
   p.name = "wonderful line";
   p.color = "0xFF3300";
}
...

Now i want to do it more “dynamically” in the way i really don’t bother with static typed member names (and possibly them accessors in the future), especially when feeding objects with data coming from the parser. I mean is easy to do it with 2 only node types but i will have more than dozens of them.

So the concept i want to do is to “statically” push vector of allowed attributes for each node type (class) and then only to do checks if this attribute is allowed in the object and set it during parsing. I possibly want to have 2 important members 1. is std::map of kv pairs, the second is the static vector of allowed attributes for certain node. Following the code typed before:

...
class DataObj {
  ...
  static std::vector<const char*> allowedAttrs;
  std::map <const char*, const char*> attrs;

  private: 
     static bool isInit;
  ...
}
...
DataObj::DataObj(){
  if(!isInit)
    allowedAttrs.push_back("name");
  isInit = true;
}
...
Layer::Layer(){
  if(!isInit) // private static for Layer
     allowedAttrs.push_back("locked");
}
...
Pline::Pline(){
  if(!isInit) // private static for Pline
     allowedAttrs.push_back("color");
}
...

The problem i’m getting here is probably visible from the moon. If we init frst a new Layer then a new Pline, Pline will have name, locked and color in the allowedAttrs vector and that’s not correct, becaue “locked” should be only valid for layer node.

So i need some way to solve this problem in the way the member “allowedAttrs” become “private” for the un-common attributes like “locked” in Layer object but also retain its “public” nature from the super class “DataObj” – so it can catch the shared attributes like “name”. In other words i don’t want to “break” the “inheritance flow” going up to the base class, and define new variable for each node class (object) repeating the same code over and over again. (something like an virtual variable).

I hope this question is not (so) dumb, and really will appreciate your answers.

  • 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-12T04:30:32+00:00Added an answer on June 12, 2026 at 4:30 am

    1) I would use seperate static members for each type to hold the allowed attributes for each type.
    2) Move the static members into functions, which is safer, and possibly avoids checking to see if it’s initialized all the time (depending on how good your compiler is and other details)
    3) Don’t use const char* except for very specific things. If you don’t know what those things are, always use std::string. In this case, we have to use std::string.
    4) I changed allowedAttrs from a vector to a set, which might be faster for large numbers of attributes, and slower for smaller numbers, maybe.

    Here’s the base:

    class DataObj {
      const std::set<std::string>& get_allowed_data_attributes() static {    
        static std::set<std::string> allowedAttrs = {"name"};
        return allowedAttrs;
      }
      std::map <std::string, std::string> attrs;
      public:
         DataObj(){ }
         void set_attribute(std::string key, std::string value) {
             auto it = get_allowed_data_attributes().find(key);
             if (it  == get_allowed_data_attributes().end())
                throw bad_key_exception(key);
             attrs.insert(std::make_pair(std::move(key), std::move(value)));
         }
         const std::string& get_attribute(const std::string& key) const {
             auto it = attrs().find(key);
             if (it  == attrs().end())
                throw bad_key_exception(key);
             return it->second;
         }
    };
    

    Here’s the derived:

    class Layer : public DataObj {
      const std::set<std::string>& get_allowed_data_attributes() static {    
        static std::set<std::string> allowedAttrs = {"locked"};
        return allowedAttrs;
      }
      public:
         DataObj(){ }
         void set_attribute(std::string key, std::string value) {
             auto it = get_allowed_data_attributes().find(key);
             if (it  == get_allowed_data_attributes().end())
                DataObj::set_attribute(std::move(key), std::move(value));
             else
                attrs.insert(std::make_pair(std::move(key), std::move(value)));
         }
         const std::string& get_attribute(const std::string& key) const {
             auto it = attrs().find(key);
             if (it  == attrs().end())
                return DataObj::get_attribute(key);
             else
                return it->second;
         }
    };
    

    Note that if you give it an invalid key, it throws a bad_key_exception, which you will have to add. Be sure it inherits from std::runtime_error.

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

Sidebar

Related Questions

I have a question about static member of class in c++, since the static
I have a question about making static libraries that use other static libraries. I
I'm a Ruby newbie. Have a very basic question about static and instance variables.
I have a general question about the new Django 1.3 static file framework. I
I have a small question about static variable and TypeObjects. I use the API
I have a question about the variables inside the static method. Do the variables
i have a question about mod_rewrite. this is my static html site architecture created
I have a question about rotating the Android device. My code logs a static
I have two questions about static + const members and static classes. One: Why
I have a question about the member variables of static struct in C language.

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.