Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is friend class and how it is used in linklist?
i have searched on google but want to listen from experts
Posted

Friend classes are able to access protected or private variables in the class that declares them a friend.

C++
class A {
	private:
		int a;
	friend class B; //This lets any code in class B access anything private in here
};

class B {
	public:
		void GetA(const A &object) {
			return object.a; //This would usually be illegal because A.a is private, but this class is a friend of A, so it is allowed
		}
};


In a linked list however, this should not be needed.
Each node in a linked list would typically all be of the same class.
 
Share this answer
 
Comments
Member 8370427 10-Nov-11 8:29am    
what you said in last two lines.can you explain it more?
Albert Holguin 15-Nov-11 12:19pm    
The two concepts are independent of one another... A friend class is a class that can access your initial class' internal private and protected members without restriction (could be dangerous). On the other hand, a linked list simply means a list of (typically a structure) that contains at least one pointer to the next item in the list (again, typically of the same exact type).
Chuck O'Toole 10-Nov-11 9:45am    
Remember, only your friend can see your privates :)
Albert Holguin 15-Nov-11 12:14pm    
lol, that's too funny!
Member 8370427 wrote:
what you said in last two lines.can you explain it more

See Wikipedia[^]for a graphic example of a linked list.

In code, this would look like:
C++
template<typename t>
class LinkedListNode {
	public:
		LinkedListNode() : m_pNext(NULL) { }
		LinkedListNode(T tData) : m_pNext(NULL), m_tData(tData) { }
		void SetNext(LinkedListNode *node) { m_pNext = next; }
		LinkedListNode *GetNext() { return m_pNext; }
		void SetData(T &tData) { m_tData = tData; };
		T &GetData() { return m_tData; };

	private:
		LinkedListNode *m_pNext;
		T m_tData;
};


This is a very basic single linked list. This example is for a linked list of integers:
C++
LinkedListNode<int> head = new LinkedListNode<int>(3);
LinkedListNode<int> next = new LinkedListNode<int>(1);
head->SetNext(next);
next->SetNext(new LinkedListNode<int>(2));
//Now print our elements
LinkedListNode<int> node = head;
while (node != NULL) {
	printf("%d\n", node->GetData());
	node = node->GetNext();
}


This gives the linked list 3->1->2 (each number would be printed on a separate line in the output)

You would add more functionality for things like add to end, which would loop through until it finds the node which has a GetNext() of NULL, and then set that next to the new node.

The point here is that every part of the linked list is of the same class, LinkedListNode.

If you still need help understanding linked lists, google[^] is your friend.
 
Share this answer
 
v2
Comments
Albert Holguin 15-Nov-11 12:16pm    
If he's new, I doubt he understands templates, perhaps not the best example to clarify the two concepts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900