c++friend-class

Vice Versa Of friendship(accessibility) in class is true or not 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?


Solution

  • 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;
    };