LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 06-15-2005, 11:10 PM   #1
lowpro2k3
Member
 
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?

Code:
class Node {
    friend class List;
...

};


...

class List {

...

};
 
Old 06-15-2005, 11:37 PM   #2
AdaHacker
Member
 
Registered: Oct 2001
Location: Brockport, NY
Distribution: Kubuntu
Posts: 384

Rep: Reputation: 31
Quote:
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-16-2005, 12:00 AM   #3
lowpro2k3
Member
 
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-16-2005 at 12:08 AM.
 
Old 06-16-2005, 11:15 AM   #4
Hivemind
Member
 
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):
Code:
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 11:18 AM.
 
  


Reply


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-05-2005 12:48 AM
cirular linked list pantera Programming 8 04-21-2005 07:59 AM
how to free linked list containers? rgiggs Programming 3 07-30-2004 02:24 PM
C++ linked list fun chens_83 Programming 2 08-04-2003 08:40 AM
book linked list in C jetfreggel Programming 14 03-16-2003 11:52 AM


All times are GMT -5. The time now is 03:36 PM.

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