Gaurav Gaurav - 2 months ago 6
C++ Question

Can A Class Can Access Members Of Its Friend Class In C++?

A friend class can access the members of the class containing the friend function. Is Its vice-versa i.e. the class can also access the members of it friend class is true?

Answer

Is Its vice-versa i.e. the class can also access the members of it friend class is true?

No, they can't. The friend keyword is unidirectional.


To provide friend functionality in a bidirectional way, you have to specify both classes as friend for each other. This actually requires a forward declaration of at least one of these candidate classes:

class B; // Forward declare
class A {
    friend class B;
};

class B {
    friend class A;
};
Comments