Note that there are some explanatory texts on larger screens.

plurals
  1. POPolymorphism and inheritance of static members in C++
    primarykey
    data
    text
    <p>I need to keep a list(vector) of children for every class, and I've got a tricky problem:</p> <pre><code>class A { protected: int a; static vector&lt;A*&gt; children; public: A(int a): a(a) {;}; virtual void AddToChildren(A* obj); virtual void ShowChildren(); virtual void Show(); }; class B: public A { protected: int b; static vector&lt;A*&gt; children; public: B(int a, int b): b(b), A(a) { A::AddToChildren(this);}; virtual void Show(); }; class C: public B { protected: int c; public: C(int a, int b, int c): c(c), B(a,b) { B::AddToChildren(this);}; virtual void Show(); }; vector&lt;A*&gt; A::children=vector&lt;A*&gt;(); vector&lt;A*&gt; B::children=vector&lt;A*&gt;(); void A::AddToChildren(A *obj) { children.push_back(obj); } void A::ShowChildren() { for(vector&lt;A*&gt;::iterator i=children.begin(); i!=children.end();i++) (*i)-&gt;Show(); } </code></pre> <p>Adding A(0), B(1,1) and C(2,2,2) and calling a.ShowChildren gives: 1,1 ; 2,2,2 ; 2,2,2</p> <p>Every time I make an instance of class C the A::children is updated instead of B::children and A::children. Sooo... the class C is added twice to the children of A class, but not added to class B. It helps when I copy the AddChildren class (<em>literally</em> copy) to class B, so that every class has its own AddChildren/ShowChildren. Also I've managed to accomplish this task using pointers, but I'm wondering is there a better way. I think that the problem is somewhere in the "using the right vector", but I don't know how to force the compiler to use the right one. </p> <p>I would be grateful for any suggestions on whatever I'm doing wrong here. </p> <p>First of all, thank you all for your comments and help. Using your advice (about my design and virtual GetList()) I managed to simplify my program:</p> <pre><code>class A { protected: int a; virtual vector&lt;A*&gt;* GetList(); public: A(int a): a(a) {;}; A(int a, A* inherited):a(a) { AddToChildren(inherited);}; static vector&lt;A*&gt; children; virtual void AddToChildren(A* obj); virtual void ShowChildren(); virtual void Show(); }; class B: public A { protected: int b; virtual vector&lt;A*&gt;* GetList(); public: static vector&lt;A*&gt; children; B(int a, int b): b(b), A(a,this){;}; B(int a, int b, A* inherited) : b(b), A(a,this){AddToChildren(inherited);}; virtual void Show(); }; class C: public B { protected: int c; public: C(int a, int b, int c): c(c), B(a,b,this) { }; virtual void Show(); virtual vector&lt;A*&gt;* GetList(); }; vector&lt;A*&gt; A::children=vector&lt;A*&gt;(); vector&lt;A*&gt; B::children=vector&lt;A*&gt;(); void A::AddToChildren(A *obj) { GetList()-&gt;push_back(obj); } void A::ShowChildren() { for(vector&lt;A*&gt;::iterator i=GetList()-&gt;begin(); i!=GetList()-&gt;end();i++) (*i)-&gt;Show(); } vector&lt;A*&gt; * A::GetList() { return &amp; children; } vector&lt;A*&gt; * B::GetList() { return &amp; children; } vector&lt;A*&gt; * C::GetList() { return &amp; children; } </code></pre> <p>Now its using constructors without calling the upper class, it just calls the proper constructor of the upper class. Its not the best, but i think it's better. </p> <p>Once more, thank you all for help. </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