Search code examples
c++oopenumsencapsulationfriend

how to enum friend in my class


i have the following two classes :

class A{
enum ee{a = 1, b = 2 , c = 3};
};

class B{
 /*


 */

};

Now i want to use enum ee in class B how i to friend enum ee in class A?\


Solution

  • you could restrict access more selectively using this approach:

    class B;
    
    class A {
        class inner {
            enum ee {a = 1, b = 2 , c = 3};
            friend class B;
        };
    public:
        typedef inner exposed;
    };
    
    class B {
        void f() {
            const A::exposed::ee e(A::exposed::a);
        }
    };
    

    this introduces restrictions above the other options, for the times you want/need to be more specific wrt access.

    specifically, A does not need to be friends with B using this approach, and the declarations in A::inner are restricted as you have specified. A::inner can then keep its declarations private, and allow access via friendship. clients could declare an inner (accessed via A::exposed), but it will be of no practical use to the client if the enum type and constants are private.