troubleshooting Question

Best way to maintain a list of objects?

Avatar of Hamlet081299
Hamlet081299 asked on
Editors IDEsC++
6 Comments2 Solutions457 ViewsLast Modified:
I'm a bit new to C++, so excuse me if this is a stupid question.

I have a class "AItem", where each AItem can contain child AItems.  I thought this should be a pretty straightforward thing to do, but clearly my understanding of C++'s pointers is letting me down.

I had thought I could just use...
    typedef std::list<AItem> AItemList;

And then define...
    AItemList children;
... in my class.

It seemed to be working, but then when I started trying to access children, I got strange results.

I reread the documentation on list::push_back and found this...
"The content of this new element is initialized to a copy of x."

Certainly not what I wanted.  I don't want it to store "a copy of x" I want it to store x!

Maybe I should just use a dynamic array?  What would you recommend?

This seems like a pretty common thing to do, having a hierarchy of object applies to all sorts of applications, so surely it should be hard!

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros