General linked-list-template for class objects

Posted on 2004-11-04
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
    LVL 4

    Accepted Solution

    Hi metellius,

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


    Author Comment

    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?
    LVL 4

    Expert Comment

    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

    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    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.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now