By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

declaring friend

P: n/a
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
Aug 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Tony Johansson" <jo*****************@telia.com> wrote in message
news:4e*********************@newsc.telia.net...
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

Aug 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.