Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-15-2005, 10:10 PM   #1
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
C++ Linked List question

I'm building a Linked List at the moment, consisting of a "Node" class and a "List" class. I'm wondering if this is an appropriate place for using a "friendship" relationship - my teacher seemed to frown on using "friends" in C++ unless we need to. My logic: the List class should be able to manipulate the Node class's pointers without function calls for every manipulation.

I'm using this kind of implementation right now, and it is working. Yay or nay?

class Node {
    friend class List;



class List {


Old 06-15-2005, 10:37 PM   #2
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 32
Originally posted by lowpro2k3
My logic: the List class should be able to manipulate the Node class's pointers without function calls for every manipulation.
Why? The rule of thumb is that you shouldn't give more access than you really need to. Do you have some good reason for ignoring this rule, or did you just arbitrarily decide that direct access is good and functions are bad?

Your teacher is right - "friend" is one of those things that you probably shouldn't be using unless you really need it, and in this case, you definitely don't need it. Sure, the code works, but there's lots of code out there that's chock full of gotos and global variables that also works. Trust me, it's easier to learn good programming practices the first time than to try to break bad habits later.
Old 06-15-2005, 11:00 PM   #3
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Original Poster
Rep: Reputation: 30
Didnt mean to post here, wrong thread (dont ask!)

Looking over my code I see where I could lose the friendship relationship. I'll wait for a few more opinions, IMO the stack overhead incurred is not worth pleasing all the OO fanatics. I do believe in it to a high extent (this -is- the first time I'm going out of my way to use it instead of get/set methods), but if I'm reading in massive files I would like to run benchmarks on both (friends vs. no friends) and see which is faster.

Maybe my other teacher is right - I try to hard to be a "hotshot"

Last edited by lowpro2k3; 06-15-2005 at 11:08 PM.
Old 06-16-2005, 10:15 AM   #4
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
For nodes I would use a private (or maybe protected) nested struct. Nodes shouldn't be visible to the user of your list class but only used internally by your class and thus a struct is fine. Remember that a struct in C++ works almost like a class, the only difference is that the default access label is public for structs and private for classes. This means a struct can have constructors and all other useful things classes have.
Say you're making a doubly-linked list class storing ints, the Node-struct could be (but usually templated of course):
struct Node
   Node() : m_next(NULL), m_previous(NULL), m_data(0) {}

   Node(int data) : m_next(NULL), m_previous(NULL), m_data(data) {}
   Node *m_next;
   Node *m_previous;
   int m_data;
As I see it, you're not violating any "OO rules" with a struct like this since it's a type that's private to the class. For types used directly by end-users, I rarely use structs, though.

Last edited by Hivemind; 06-16-2005 at 10:18 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
cirular linked list pantera Programming 8 04-21-2005 06:59 AM
how to free linked list containers? rgiggs Programming 3 07-30-2004 01:24 PM
C++ linked list fun chens_83 Programming 2 08-04-2003 07:40 AM
book linked list in C jetfreggel Programming 14 03-16-2003 10:52 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration