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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:03:29+00:00 2026-05-13T07:03:29+00:00

I need to keep a list(vector) of children for every class, and I’ve got

  • 0

I need to keep a list(vector) of children for every class, and I’ve got a tricky problem:

class A
{
protected:
    int a;
    static vector<A*> 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<A*> 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<A*> A::children=vector<A*>();
vector<A*> B::children=vector<A*>();


void A::AddToChildren(A *obj)
{
    children.push_back(obj);
}

void A::ShowChildren()
{
    for(vector<A*>::iterator i=children.begin(); i!=children.end();i++)
        (*i)->Show();
}

Adding A(0), B(1,1) and C(2,2,2) and calling a.ShowChildren gives: 1,1 ; 2,2,2 ; 2,2,2

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 (literally 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.

I would be grateful for any suggestions on whatever I’m doing wrong here.

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:

class A
{
protected:
    int a;
    virtual vector<A*>* GetList();
public:
    A(int a): a(a) {;};
    A(int a, A* inherited):a(a) { AddToChildren(inherited);};

    static vector<A*> children; 

    virtual void AddToChildren(A* obj);
    virtual void ShowChildren();

    virtual void Show();
};

class B: public A
{
protected:
    int b;
    virtual vector<A*>* GetList();
public:
     static vector<A*> 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<A*>* GetList();
};


vector<A*> A::children=vector<A*>();
vector<A*> B::children=vector<A*>();


void A::AddToChildren(A *obj)
{
    GetList()->push_back(obj);
}

void A::ShowChildren()
{
    for(vector<A*>::iterator i=GetList()->begin(); i!=GetList()->end();i++)
        (*i)->Show();
}


vector<A*> * A::GetList()
{

    return & children;
}

vector<A*> * B::GetList()
{

    return & children;
}

vector<A*> * C::GetList()
{

    return & children;
}

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.

Once more, thank you all for help.

  • 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-05-13T07:03:29+00:00Added an answer on May 13, 2026 at 7:03 am

    Your design intent is not sufficiently clear, which is why the authors of some other answers got confused in their replies.

    In your code you seem to make calls to AddToChildren from some constructors but not from the others. For example, you have a children list in A but you never call the AddToChildren from A::A constructor. Also, class C has no its own children list. Why? Is it supposed to share the children list with B?

    I can guess that the fact that you are not calling AddToChildren from all constructors means that some constructors are intended to build complete “objects” of given type (these constructors do call AddToChildren), while some other constructors are intended to be used as “intermediate” constructors by descendant classes (these constructors don’t call AddToChildren).

    Such design might be considered quite questionable and error prone. Note, for example, that C::C calls AddToChildren, which supposedly is adding this to B::children (was it the intent?), and also invokes B::B constructor, which will also add this to B::children. So, the same this value is added to the list twice. This does not seem to make any sense.

    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 GetList 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).


    (Taking into account the OP’s clarifying comments)

    So, you want B objects to be added to A::children list and C objects to be added to both A::children and B::children lists. This can be achieved by

    class A {
      ...
      int a;
      static vector<A*> children;
      ...
      A(int a) : a(a) {}
    
      virtual vector<A*> *GetList() = 0;
    
      void AddToChildren(A* obj) { // note: non-virtual
        GetList()->push_back(obj);
      }
      ...
    };
    
    class B : public A {
      ...
      int b;
      static vector<A*> children;
      ...
      B(int a, int b) : b(b), A(a) { 
        AddToChildren(this);
      }
    
      virtual vector<A*> *GetList() {
        return &A::children;
      }
      ...
    };
    
    class C : public B {
      ...
      int c;
      ...
      C(int a, int b, int c) : c(c), B(a,b) { 
        AddToChildren(this);
      };    
    
      virtual vector<A*> *GetList() {
        return &B::children;
      }
      ...
    };
    

    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 AddToChildren virtual, the virtuality of GetList alone is sufficient.

    Also, the whole thing makes little if AddToChildren just does a push_back. There’s no much sense the build such infrastructure for such a “thin” AddToChildren alone. Just do what you want to do explicitly in each constructor.

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

Sidebar

Related Questions

I've got a list of number that I need to keep track of. The
I need to keep a list of user_id s that have viewed a piece
In my application I need to keep track of a list of objects that
I have sorted collection (List) and I need to keep it sorted at all
I need to keep a list to simulate memory management. The program assigns a
At our company, I need to keep a web page up with a list
I need to keep leading zeros on a list of numbers. The numbers are
I have a class called PriceStep . I keep a list of PriceStep objects
I need to keep selected the options users choose in a multiple select menu.
I need to keep a stack of 10 items (value primitives, not objects) where

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.