473,899 Members | 3,984 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get enum from a different namespace.

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
};
}
now in another namespace,
using test::MyEnum; //It gets myenum.

MyEnum e = VALUE1; //It doesnt get the value.
One solution is to have using test::VALUE1;
but as such enum const's are huge in number it is not possible to use
all of them seperately.
Other way is to have using namespace test; But the namespace has many
other things and I want to avoid such statement if possible.

It looks the enum constants are 'free' inside the namespace, not
guarded by the MyEnum.

Anyway to deal with it? Is it possible to enclose them inside a dummy
class and use them ?

Sep 21 '06
13 18187

Jens Theisen wrote:
Comeau and gcc disagree, so I guess it's nonstandard. What compiler do
you use?
I think so too.
>
I don't know of a way to practically forward declare an enum in a
standard way or a feasible workaround. If someone does, I'd like to
hear.
I don't think there is one. I don't know why, but think there may be
good reason. Maybe some std guru can comment :-). The fact that comeau
does not compile it, makes me wonder. I know VCC does compile it, but I
work on more than one compiler - 4 to be precise (5 comeau included).
An alternative is to wrap it in a namespace:
The problem with wrapping in a namespace, is that each value that forms
part of the enum must be explicitly qualified (I think).

therefore...

using MyEnum::enum_t;

int x = static_cast<int >(eSome); //error, no type eSome
// in scope, required to qualify with MyEnum...

Regards,

Werner

Sep 22 '06 #11
"werasm" <w_*****@telkom sa.netwrites:
The problem with wrapping in a namespace, is that each value that forms
part of the enum must be explicitly qualified (I think).

therefore...

using MyEnum::enum_t;

int x = static_cast<int >(eSome); //error, no type eSome
// in scope, required to qualify with MyEnum...
Not if you use a using directive:

using namespace MyEnum;
^^

Regards,

Jens
Sep 22 '06 #12

Jens Theisen wrote:
"werasm" <w_*****@telkom sa.netwrites:
Not if you use a using directive:

using namespace MyEnum;
Yes, true. Unfortunately this is not a good idea in an .h file (for
qualification in class definitions). In general, it is not common to
use enum values in class definitions, though. If the need was required,
using an anonymous enum with a class is quite handy.
struct someS{
typedef x::y::z enum_t enum_t;

//now values qualified here by enum_t::value;
};

I have also found that the class name can form part of the actual enum
name (nice feature for categorisation) . This could be true for
namespaces too, though.

struct State
{
enum
{
Standby, Maintenance, Operational, Shutdown
};
};

Now qualify like:

State::Standby etc... The qualifier name conveys information about the
category of the enumerated type. A feature we often use. The biggest
reason for doing this though, is the fact that forward declaration of
enums don't work. If that was not the case, I would probably consider
using namespaces to scope my enums like you propose.

Regards,

Werner

Sep 22 '06 #13

Jens Theisen wrote:
"mlimber" <ml*****@gmail. comwrites:
This is entirely unnecessary since you can forward-declare enums
without the struct:

Comeau and gcc disagree, so I guess it's nonstandard. What compiler do
you use?
How about that? Comeau and gcc do issue a warning, but VC++ 6, 7, 7.1,
8, and (most surprisingly) EDG don't.
I don't know of a way to practically forward declare an enum in a
standard way or a feasible workaround. If someone does, I'd like to
hear.
How about the technique presented here:
http://www.ddj.com/dept/cpp/184403894

Cheers! --M

Sep 22 '06 #14

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

Similar topics

4
642
by: Marshall Mills | last post by:
As I understand it, loaded statement, a using declaration should be all I need to see an enum from within a namespace. The below code works fine with class, struct, and union. What gives? As the code says, if I employ the using directive, I'm ok. /* built with Visual C++ 6, SP 5 */ namespace Traffic { enum Light { red, yellow, green }; }
21
6395
by: Bilgehan.Balban | last post by:
Hi, I have two different enum definitions with members of same name. The compiler complains about duplicate definitions. Is this expected behaviour? Can't I have same-named fields in different enum definitions? I think it should have been perfectly valid to do that. Its a stupid limitation to have to define disjoint enum symbol definitions. This is like having to have disjoint member names in structures.
2
5702
by: msnews.microsoft.com | last post by:
How do I get Intellisense to open a dropdown list box for a method's parameters when the parameter is an ENUM? public class MyClass { public enum IDkind { PersonID, EntityID, PlaceID
8
1726
by: Joe | last post by:
I have a .cs file which is linked to several other projects. All my classes in this file are defined as internal. I would like to have an enum defined as well in the namespace but I get an error from one project that the accessibility of the enum is less (public) than that of a method which returns that's type. What is the best way to define this enum without getting duplicate define warnings or this error of less accessibility? ...
5
2680
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer references the Data layer, but I would like to have the enum exposed to all three. Is there a way to trickle down that enum (Like class inheritance exposes classes) so that it can be accessed from the Business layer and UI layers? My UI layer doesn't...
3
2555
by: toton | last post by:
I have an enum in a namespace like, namespace client{ namespace ui{ enum InkEnum{ ID_INK_COLOR, ID_INK_WIDTH, }; class InkEventHandler{ ... };
34
11229
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
6728
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
3052
by: hufaunder | last post by:
Imagine you have a charting library that can draw lines, bars, floating bars, bands, etc. Lines and bars need only one input. Floating bars and bands need two inputs. There are two approaches: 1) One enum with all 4 types (bars, band, etc). One chart class that accepts up to 2 arrays of values. If the user choses a band but there is only one input array throw an exception. If the user passes two input arrays with different lengths throw...
0
9997
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...
1
10966
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
10493
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
9666
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...
1
8039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7201
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
6081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4299
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3316
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.