Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friend scope in C++

Tags:

If I have three classes, A, B, C. A and B are friends (bidirectionally). Also, B and C are friends (bidirectionally). A has a pointer to B and B has a pointer to C. Why can't A access C's private data through the pointer?

Just to clarify: This is a pure theoretical C++ language question, not a design advice question.

like image 941
Marcin Avatar asked Jan 12 '09 22:01

Marcin


People also ask

What is the scope of friend function?

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

What is friend function C?

A friend function is a function that isn't a member of a class but has access to the class's private and protected members.

What is a friend class in C?

A friend class in C++ can access the private and protected members of the class in which it is declared as a friend. A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure.

Is friend a keyword in C?

friend is a keyword in C++ that is used to share the information of a class that was previously hidden. For example, the private members of a class are hidden from every other class and cannot be modified except through getters or setters.


2 Answers

Friendship in C++ is not transitive:

John is a friend of mine and he can use my wireless connection any time (I trust him).
John's friend Tim though is a waster and though John is my friend I do not include Tim as a friend, and thus I don't let him use my wireless connection.

Friendship is NOT inherited

Also John's children are a bunch of hooligans so I don't trust them either they are definitely not my friends nor are my own children who I trust as far as I could throw them.

Though our children can not directly accesses the wireless they can get access to it if they go through us. So John's children can access my wireless if they access it via John (ie they are supervised and protected by John).

Also, friendship is not symmetric.

John has a goverment job so he unfortunately is not allowed to trust anyone, especially when it comes to wireless.

You are always your own best friend.

This allows things like copy constructors where you can access the private member of another object even though there is no real accesses.

So I am also automatically friends with all my clones :-) as they are just other instances of myself.

like image 57
Martin York Avatar answered Dec 09 '22 20:12

Martin York


Friendship in C++ is not transitive:

(A is friend of B) and (B is friend of C) does not mean (A is friend of C)

Also, friendship is not symmetric.

(A is friend of B) does not mean (B is friend of A) 

You have to explicitly state that A is a friend of C to be able to access C's private stuff from within A. If adding a setter and getter to a class exposes information not meant to be exposed, you should consider friends if you can't find your design being faulty (using friend is valid. It's not a sign for bad design). If you can add a setter and getter without that being destructive to the interface, then you should avoid making other classes friends. Note that a nested class is always a friend of the nesting class. So a nested class can see the privates of the nesting class.

like image 33
Johannes Schaub - litb Avatar answered Dec 09 '22 20:12

Johannes Schaub - litb