Is there an elegant way to share enum between classes?

Discussion in 'C++' started by Pierre Couderc, Dec 4, 2004.

  1. I want to share enum between 2 classes C1 and C2:

    class C1
    {
    public:
    enum shut {AAA,BBB, CCC};
    shut m_C1;
    void set(shut &s) { m_C1=s;}
    };

    class C2 : public Cother
    {
    public:
    enum shut {AAA,BBB, CCC};
    shut m_C2;
    };

    void main()
    {
    C1 a;
    C2 b;
    a.set(AAA);
    b.m_C2 = a.m_C1;
    }

    Is there a more elegant way than the one I have found, but for which I
    do not measure the side effects :

    class Cenum
    {
    public:
    enum shut {AAA,BBB, CCC};
    };

    class C1 : public Cenum
    {
    public:
    shut m_C1;
    void set(shut &s) { m_C1=s;}
    };

    class C2 : public Cother, Cenum
    {
    public:
    shut m_C2;
    };
     
    Pierre Couderc, Dec 4, 2004
    #1
    1. Advertising

  2. Pierre Couderc

    Rolf Magnus Guest

    Pierre Couderc wrote:

    > I want to share enum between 2 classes C1 and C2:
    >
    > class C1
    > {
    > public:
    > enum shut {AAA,BBB, CCC};
    > shut m_C1;
    > void set(shut &s) { m_C1=s;}
    > };
    >
    > class C2 : public Cother
    > {
    > public:
    > enum shut {AAA,BBB, CCC};
    > shut m_C2;
    > };
    >
    > void main()
    > {
    > C1 a;
    > C2 b;
    > a.set(AAA);
    > b.m_C2 = a.m_C1;
    > }
    >
    > Is there a more elegant way than the one I have found, but for which I
    > do not measure the side effects :


    Yes. You don't need to make an enum a class member. Just put it on namespace
    scope, and both classes can easily use them.
     
    Rolf Magnus, Dec 4, 2004
    #2
    1. Advertising

  3. Rolf Magnus wrote:
    > Pierre Couderc wrote:
    >
    >
    >>I want to share enum between 2 classes C1 and C2:
    >>
    >>class C1
    >>{
    >>public:
    >>enum shut {AAA,BBB, CCC};
    >>shut m_C1;
    >>void set(shut &s) { m_C1=s;}
    >>};
    >>
    >>class C2 : public Cother
    >>{
    >>public:
    >>enum shut {AAA,BBB, CCC};
    >>shut m_C2;
    >>};
    >>
    >>void main()
    >>{
    >>C1 a;
    >>C2 b;
    >>a.set(AAA);
    >>b.m_C2 = a.m_C1;
    >>}
    >>
    >>Is there a more elegant way than the one I have found, but for which I
    >>do not measure the side effects :

    >
    >
    > Yes. You don't need to make an enum a class member. Just put it on namespace
    > scope, and both classes can easily use them.
    > ,

    Thank you, sure I can put it at at a higher scope, but this may conflict
    with other declaration at higer level, obliging me to declare :
    enum shut { SHUT_OK, SHUT_ERROR, SHUT_AAA, SHUT_BBB...}

    My idea is to keep programation simpler or shorter inside these (friend
    or not) classes : OK, ERROR, AAA,BBB...
     
    Pierre Couderc, Dec 4, 2004
    #3
  4. Pierre Couderc

    Arijit Guest

    Pierre Couderc wrote:

    > <snip>
    > Thank you, sure I can put it at at a higher scope, but this may conflict
    > with other declaration at higer level, obliging me to declare :
    > enum shut { SHUT_OK, SHUT_ERROR, SHUT_AAA, SHUT_BBB...}
    >
    > My idea is to keep programation simpler or shorter inside these (friend
    > or not) classes : OK, ERROR, AAA,BBB...


    Thats why namespaces are there, to avoid such conflicts. Put the class
    and enum declarations within a namespace.

    -Arijit
     
    Arijit, Dec 4, 2004
    #4
  5. Pierre Couderc

    Paavo Helde Guest

    Pierre Couderc <> wrote in news:cos6kh$1i9u$1
    @biggoron.nerim.net:

    > I want to share enum between 2 classes C1 and C2:
    >

    One option:

    namespace shut_t {
    enum shut {AAA,BBB, CCC};
    }

    class C1 {
    public:
    typedef shut_t::shut shut;
    void set(shut &s);
    shut m_C1;
    // ...
    };

    class C2 {
    public:
    typedef shut_t::shut shut;
    shut m_C2;
    // ...
    };

    int main() {
    C1 a;
    C2 b;
    a.set(shut_t::AAA);
    // or with: using namespace shut_t;
    a.set(AAA);

    b.m_C2 = a.m_C1;
    }

    Regards
    Paavo

    > class C1
    > {
    > public:
    > enum shut {AAA,BBB, CCC};
    > shut m_C1;
    > void set(shut &s) { m_C1=s;}
    > };
    >
    > class C2 : public Cother
    > {
    > public:
    > enum shut {AAA,BBB, CCC};
    > shut m_C2;
    > };
    >
    > void main()
    > {
    > C1 a;
    > C2 b;
    > a.set(AAA);
    > b.m_C2 = a.m_C1;
    > }
    >
    > Is there a more elegant way than the one I have found, but for which I
    > do not measure the side effects :
    >
    > class Cenum
    > {
    > public:
    > enum shut {AAA,BBB, CCC};
    > };
    >
    > class C1 : public Cenum
    > {
    > public:
    > shut m_C1;
    > void set(shut &s) { m_C1=s;}
    > };
    >
    > class C2 : public Cother, Cenum
    > {
    > public:
    > shut m_C2;
    > };
    >
     
    Paavo Helde, Dec 4, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jan De Ceuster
    Replies:
    5
    Views:
    4,993
    Thomas Stanka
    Jan 13, 2005
  2. Kamilche
    Replies:
    7
    Views:
    300
    Peter Hansen
    Jun 29, 2004
  3. volcano
    Replies:
    3
    Views:
    284
    volcano
    Aug 24, 2006
  4. oaksong

    share enum between pages?

    oaksong, Sep 17, 2008, in forum: ASP .Net
    Replies:
    8
    Views:
    382
    oaksong
    Sep 19, 2008
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,427
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page