[TriLUG] OT: C++ question, linked list of objects

Joseph Mack NA3T jmack at wm7d.net
Sun Apr 2 16:14:46 EDT 2006


This is part of homework for a course I'm doing. I'm 
required to make a linked list of objects. I can do a linked 
list of structs, but when I do it with objects, object->next 
is private, which makes me think that I'm on the wrong 
track. Part of the homework is figuring out how to have a 
class as a node in a linked list.

Google gives me stl libraries in template format, which I 
don't understand. I looked at newsgroups - it seemed to be 
either off-topic or I didn't understand it. All the webpages 
I've looked at have structs as the nodes in a linked list. 
(Why we have to figure this out ourselves, rather than being 
given some guidance in lectures and then go off and 
implement it is another matter.)

I'm new at C++, can't do templates etc, so the explanation 
will have to be at a simple level.

Here's the object class

//Submarine.h
class Submarine {

private:
    int num_engines;
    //long list of private attributes
    .
    .
    Submarine* next;

public:
};


Here's the list class

//List.h
class List{

private:
    Submarine* head;
    Submarine* tail;
    int count; //number of submarines
    ???

What should go in the "???" so that I can do something like

public:

    void insert(const Submarine& submarine){
       .
       .
       Submarine* current;
       submarine.next = current; //problem: next is private,
                                 //can use accessors but seems clunky
       .
    }

}; //List class

Am I supposed to use accessors or have I got the design 
completely wrong?

Thanks Joe

-- 
Joseph Mack NA3T EME(B,D), FM05lw North Carolina
jmack (at) wm7d (dot) net - azimuthal equidistant map
generator at http://www.wm7d.net/azproj.shtml 
Homepage http://www.austintek.com/ It's GNU/Linux!



More information about the TriLUG mailing list