Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is there an elegant way to share enum between classes?

Reply
Thread Tools

Is there an elegant way to share enum between classes?

 
 
Pierre Couderc
Guest
Posts: n/a
 
      12-04-2004
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;
};
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      12-04-2004
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.

 
Reply With Quote
 
 
 
 
Pierre Couderc
Guest
Posts: n/a
 
      12-04-2004
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...

 
Reply With Quote
 
Arijit
Guest
Posts: n/a
 
      12-04-2004
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

 
Reply With Quote
 
Paavo Helde
Guest
Posts: n/a
 
      12-04-2004
Pierre Couderc <(E-Mail Removed)> 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;
> };
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Difference between enum class and enum struct Brian C++ 4 02-27-2010 04:03 PM
Is there an elegant way to dir() module from inside? volcano Python 3 08-24-2006 09:10 PM
Re: Is there an elegant way to set an unsigned vector to 1 Jan De Ceuster VHDL 5 01-13-2005 07:26 AM
Is there a more elegant way to do this? Kamilche Python 7 06-29-2004 04:33 AM



Advertisments