Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?\

like image 469
PersianGulf Avatar asked Sep 16 '25 16:09

PersianGulf


2 Answers

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.

like image 108
justin Avatar answered Sep 18 '25 09:09

justin


If you put the enum in the public section of A, you can refer to it as A::ee, and the values as A::a, A::b, and A::c.

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

class B
{  
    A::ee an_enum;
    /*    */
}; 
like image 37
Bo Persson Avatar answered Sep 18 '25 08:09

Bo Persson