General linked-list-template for class objects

Posted on 2004-11-04
Medium Priority
Last Modified: 2013-12-14
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.
Question by:metellius
  • 2
  • 2

Accepted Solution

pankajtiwary earned 1000 total points
ID: 12500376
Hi metellius,

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


Author Comment

ID: 12500503
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?

Expert Comment

ID: 12500973
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.


Author Comment

ID: 12503054
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?

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question