General linked-list-template for class objects

When making games i've been using linked lists to have dynamic class-instantiation, and this has worked very well, but in the long run, I feel it's too complicated having to cut and paste the code when making a list for another linked list. I've been looking at some alternatives, but I need some tips on what would suit me best.

What i need is a way to easily create a linked-list object that keeps track of all the list-objects i've created, as well as a way to easily loop through all the objects and perform some action on them.

I've considered using the STL list or vector-templates, but I seems like its not really what I want as I have to specify an object to add, rather than telling it to add an object for itself at the end of the linked list. (To me, the whole point with linked lists is being able to add objects dynamically without instantiating an object first)

The other alternative I thought about is making a class template for a general list-control. Then I can inherit from that class template, and make my own list-control with my own add-object-function and loop-through-objects function that lets me do the instantiating and usage of the objects.
metelliusAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pankajtiwaryCommented:
Hi metellius,

For all your purpose std::list is your friend. It can do whatever you want. Read the documentation properly.

Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
metelliusAuthor Commented:
OK, i read some things about it and discovered I could actually do list.push_back(new object), which solved my problem. But maybe I can ask another question while Im here, i was wondering what happens to the constructor of a template class when I instantiate the template? Will it be copied to the new class, or will it have to be overwritten?
0
pankajtiwaryCommented:
Its good that you finally got it. Regarding your second question:

Whenever you instantiate an object from a  template class, it's code is generated in the compile time itself for all the types for which the instantiation is done.

Lets say you have a template class as template <typename T> class List { /* various members */ };
And you try to create a List as List<int> L, a new class will be generated which will be specially used to instantiate your object L. Similarly List<char> c will create another class for you.

Remember one thing, C++ has not evolved at the cost of performance (for which C is known for). The compile time code generation gives the user the flexibility to write single code for generic types *without* the overhead of runtime processing for types. So in the example both (List<int> and List<char>) will have their own constructor which will be created on the basis of the template class constructor and the type you have provided. So, you will have separate constructors for the two classes.

HTH
0
metelliusAuthor Commented:
Im not sure if you understood my question, I was wondering whether or not the constructor written for a self-written class template will be inherited into the instantiation of that template? I did a template for a general class with its constructor, but once I instantiated it, the compiler started asking where the constructor for the instantiated class was, while I wanted it to use the one already written.

Another quick question, if I use say list<class*> for a list containing pointers to the instantiations of the class car, and I instantiate objects by doing list.push_front(new car), what will then happen when I do list.pop_front()? Will it create a huge memory leak, or will the list object actually clean up the objects it points to?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.