Friend Declarations
In this lesson, we'll study friend declarations and how they are useful in classes.
We'll cover the following
The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears. Friends have access to all members of a class.
A class can declare friendship to a function, a method, or a class.
Rules:
- The declaration of a friendship can be anywhere.
- The access specifier of the friendship declaration is not relevant.
- Friendship cannot be inherited (your friend’s children are not your friends).
- Friendship is not transitive (a friend of your friend is not your friend).
Access specifiers have no effect on the meaning of friend declarations (they can appear in private:
or in public:
sections, with no differences).
A friend has full control of the internals of a class.
Get hands-on with 1400+ tech skills courses.