declaring friend

T

Tony Johansson

Hello experts!

Assume I have this class definition of class ListElem.
I'm a bit unsure how to interpret when you put friend declaration in public,
protected and private section of a class definition. If you insted have
declared a primitive type or a class type then I would understand it
completely.
I assume that having a friend declaration in the private section make no
sense.
I assume that you always put friend declaration in the public section.

Example 1
class ListElem
{
public:
friend class List
.. . .
};

Example 2
class ListElem
{
protected:
friend class List
.. . .
};

Exampl 3
class ListElem
{
private:
friend class List
.. . .
};

Many thanks

//Tony
 
B

benben

Tony Johansson said:
Hello experts!

Assume I have this class definition of class ListElem.
I'm a bit unsure how to interpret when you put friend declaration in
public, protected and private section of a class definition. If you insted
have declared a primitive type or a class type then I would understand it
completely.

No difference where you declare a friend in the class.
I assume that having a friend declaration in the private section make no
sense.
I assume that you always put friend declaration in the public section.

By declaring something as a friend you are granting permission for that
something to access the private part of the class. A sole friend statement
encapsulates nothing and therefore is not bound by access permissions.
Logically a friend (class or function) is an augmentation of a class's
public interface, and it seems natural to put that in public. However,
unlike member functions, you don't need to know the class to use its friend,
so it is okay to put the friend statement in private.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top