Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic members inheritance and protection
    primarykey
    data
    text
    <p>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.</p> <p>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:</p> <p>Suppose i chose <strong>DataObj</strong> 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:</p> <pre><code>/// 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"; } ... </code></pre> <p>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.</p> <p>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:</p> <pre><code>... class DataObj { ... static std::vector&lt;const char*&gt; allowedAttrs; std::map &lt;const char*, const char*&gt; 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"); } ... </code></pre> <p>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.</p> <p>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). </p> <p>I hope this question is not (so) dumb, and really will appreciate your answers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload