473,791 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there an elegant way to share enum between classes?

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;
};
Jul 22 '05 #1
4 3807
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.

Jul 22 '05 #2
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...

Jul 22 '05 #3
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

Jul 22 '05 #4
Pierre Couderc <pi****@couderc .ccNOSPAM> wrote in news:cos6kh$1i9 u$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::A AA);
// 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;
};


Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2165
by: Kenneth | last post by:
I want to define an enum for possible languages in my application. My enum look like this: Public Enum Languages English = 0 Portuguese = 1 End Enum How can i reuse this enum in the rest of my application (in different classes in different projects)?
5
1708
by: Kaila | last post by:
I'm using metroworks codewarrior and can't solve a redeclariotin of type problem I have several classes and each of them need to have a custom type called enum X now what I was thinking is to put the type in a seperate file and simply attach it as a librabry to my main as follows: In main.cpp
3
2000
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the invoked member function. It is possible to move the position into the object by adding a data...
4
3304
by: moondaddy | last post by:
I have some pubic Enums I like to use throughout my application. Now I'm parsing the application out into smaller DLLs or projects. How an I share a common Enum across all the projects? Thanks. -- moondaddy@nospam.com
0
2591
by: aloneplayer | last post by:
Hi, All, I wrote a function to share a folder to individual user account with WMI. When I call it : ShareFolder("c:\\test", "Test", "Shared by Riven", 8, Environment.MachineName, "Administrator"); The folder will be shared, but nobody has permission to access it. (The share permissions list of this folder is empty) Could you help me?
13
18160
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little detail, the enum is declared as, namespace test{ enum MyEnum{ VALUE1,VALUE2 };
34
11205
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
12
6724
by: Cmtk Software | last post by:
I'm trying to define an enum which will be used from unmanaged c++, C++/CLI managed c++ and from C#. I defined the following enum in a VS dll project set to be compiled with the /clr switch: public enum Days { Sunday };
3
8006
by: Dean Mitchell | last post by:
Hi everyone, We have a c++ server application that we are writing a GUI client application for. To save our time and to avoid duplicating all the code and functionality that already exists in c++ classes I am building a managed c++ assembly around these classes so that a c# program can use them. The problem is that alot of these c++ classes use enums, I would like to make these visible to the c# application but I cannot find a way to...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10154
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.