Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your design intent is not sufficiently clear, which is why the authors of some other answers got confused in their replies.</p> <p>In your code you seem to make calls to <code>AddToChildren</code> from some constructors but not from the others. For example, you have a <code>children</code> list in <code>A</code> but you never call the <code>AddToChildren</code> from <code>A::A</code> constructor. Also, class <code>C</code> has no its own <code>children</code> list. Why? Is it supposed to share the <code>children</code> list with <code>B</code>?</p> <p>I can guess that the fact that you are not calling <code>AddToChildren</code> from all constructors means that some constructors are intended to build complete "objects" of given type (these constructors do call <code>AddToChildren</code>), while some other constructors are intended to be used as "intermediate" constructors by descendant classes (these constructors don't call <code>AddToChildren</code>). </p> <p>Such design might be considered quite questionable and error prone. Note, for example, that <code>C::C</code> calls <code>AddToChildren</code>, which supposedly is adding <code>this</code> to <code>B::children</code> (was it the intent?), and also invokes <code>B::B</code> constructor, which will also add <code>this</code> to <code>B::children</code>. So, the same <code>this</code> value is added to the list twice. This does not seem to make any sense.</p> <p>You need to figure out what is it you are trying to do and then fix your design. Once you are done with it, you can "virtualize" the list using the technique proposed by Neil (introducing a virtual <code>GetList</code> method). Neil later wrote incorrectly that it will not work. In fact, it will work perfectly fine (again, assuming that I understand your intended design correctly).</p> <hr> <p>(Taking into account the OP's clarifying comments)</p> <p>So, you want <code>B</code> objects to be added to <code>A::children</code> list and <code>C</code> objects to be added to both <code>A::children</code> and <code>B::children</code> lists. This can be achieved by</p> <pre><code>class A { ... int a; static vector&lt;A*&gt; children; ... A(int a) : a(a) {} virtual vector&lt;A*&gt; *GetList() = 0; void AddToChildren(A* obj) { // note: non-virtual GetList()-&gt;push_back(obj); } ... }; class B : public A { ... int b; static vector&lt;A*&gt; children; ... B(int a, int b) : b(b), A(a) { AddToChildren(this); } virtual vector&lt;A*&gt; *GetList() { return &amp;A::children; } ... }; class C : public B { ... int c; ... C(int a, int b, int c) : c(c), B(a,b) { AddToChildren(this); }; virtual vector&lt;A*&gt; *GetList() { return &amp;B::children; } ... }; </code></pre> <p>Note that despite what was said by other posters, virtual calls do work here and they work exactly as we need them to work to achieve the requested functionality. Note though, that in this case there's no point to make method <code>AddToChildren</code> virtual, the virtuality of <code>GetList</code> alone is sufficient.</p> <p>Also, the whole thing makes little if <code>AddToChildren</code> just does a <code>push_back</code>. There's no much sense the build such infrastructure for such a "thin" <code>AddToChildren</code> alone. Just do what you want to do explicitly in each constructor.</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.
 

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