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

Barry Gaskins barry.gaskins at gmail.com
Sun Apr 2 21:22:09 EDT 2006


   Well the linked list object needs to be able to get and set the next
pointer of the node object.  Making the next member of the node public or
defining public get and set methods is the easiest way to do this. Rodney is
correct that this creates the ability for any class to mess with the value
of the next pointer which could cause problems if you or somebody writes
code that changes the next pointer directly without going through the linked
list object.  If your instructor has not gone over all these concepts then
he may not expect you to worry about this.

   If this is a more advanced class (which I doubt since you are asking such
basic questions), then you could make the next member of the node class
private but declare the  linkedList class to be a friend class of the node
class so that I can access the private next member of the node class.  That
would prevent other classes for messing with the next pointer of a node
without going through the LinkedList class.
You can see an example of doing this here:
http://cis.stvincent.edu/swd/lists/lists.html

And example of just making the next member of the node public can be found
here:
http://www.codeproject.com/cpp/Lists_and_Trees.asp

   If I were you I would start out with the simpler way of doing it with the
next member of the node class as a public member.  Then after you have
everything working correctly if you have the time and want to get extra
credit then you can try to do the harder example of making the next member
of the node object private.

   - Barry Gaskins


On 4/2/06, Joseph Mack NA3T <jmack at wm7d.net> wrote:
>
> On Sun, 2 Apr 2006, Rodney Radford wrote:
>
> >
> > I'd have to disagree with making next public, especially
> > if this is an academic assignment (most profs don't like
> > publics, and for good reason - they make later
> > modification of the classes more difficult due to higher
> > maintenance).
>
> I thought it was about maintaining encapsulation. I assume
> then that higher maintenance is one of the problems of lack
> of encapsulation. So far encapsulation has just been a
> principle.
>
> > How about turning the tables around.. instead of setting
> > the next of the parameter object to point to yourself,
>
> er, um, yourself? (*current).getNext() points to NULL if the
> node is the tail, or undefined if there are no nodes, or to
> the next node if not the last. What is "yourself"?
>
> > set your next to point to the parameter object.  That way
> > you only need to know your own next variable, and nothing
> > needs to be made public.
>
> sorry, this went over my head. Could you explain more.
> Thanks
>
> > Then, to walk the list, expose a method to traverse
> > forward one node.
>
> yes this is what I'm about to do
>
> 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!
> --
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG Organizational FAQ  : http://trilug.org/faq/
> TriLUG Member Services FAQ : http://members.trilug.org/services_faq/
>



More information about the TriLUG mailing list