Advertisement

(C++) Using '::' to get enum members?

Started by February 06, 2005 11:32 AM
8 comments, last by Null and Void 19 years, 9 months ago
In C++, we can't use '::' to get enum members, am I right? With the following declaration: enum Result { Right, Wrong, }; We can't do this: Result res = Result::Right; Is there a way to overcome this?
The Sands of Time Are Running Low
The :: is the scope resolution operator. Use it like:
class Result{public:  typedef enum an_enum  {    Right,    Wrong  } AnEnum;};Result::AnEnum a = Result::Right;

or, like thie:
// in a headernamespace Result;typedef enum an_enum  {    Right,    Wrong  } AnEnum;// in sourceResult::AnEnum a = Result::Right;// orusing namespace Result;AnEnum a = Right;

Skizz
Advertisement
Result is a type, not a namespace/scope.
I wanted todo the same thing a while back, so I wrapped the values in a namespace and then typedef'd the type so I could refer to it for parameter reasons

edit: yeah, what Skizz said
You're correct. The way to overcome this is to put the enum in a class/struct or use a namespace, e.g.

struct Result
{
enum types {Right, Wrong};
};

However, now when you want to pass the enum in as an argument, you need to type the struct name first, here's a silly example:
struct Result{    enum types {Right, Wrong};};void MyFunction(Result::types result){	if (result == Result::Right)	{		std::cout << "You were right!" << std::endl;	}	else	{		assert(result == Result::Wrong); //Shouldn't happen unless we added more types to the enum and forgot to update this peice of code!		std::cout << "You were wrong :(" << std::endl;	}}


HTH
OK, the AP was me + beaten by skizz + _the_phantom_
Also note that in C++/cli the above code would have to be something like

if(res == Result::types::Right) {}

Full qualification of the enum member is needed. So Deckards, your first example would be the prefered solution with c++/cli. Just in case.
Advertisement
Quote: Original post by Deckards
Is there a way to overcome this?


Unlikes class types (struct, class, union), enumerations do not create a scope. Use a namespace or a class type wrapper.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Wait a minute, can't you just do this:
// somefile.henum Result{    Right,    Wrong,};// somefile.cpp...void Function(){   Result res = Right;}


Abeit unsafe I know due to collisions, but it compiled for me?

- Drew
Thanks for your answers.

Nesting the enum in another type resolves the readability problem but we still have to use "Result::types" which is not very nice.

I think I'll stick witch the following approach:
enum Result
{
Result_Right,
Result_Wrong,
};
The Sands of Time Are Running Low
I cheat, here's a simplified version of what I use (ignore the seperate operator and constructor macros, they're just part of my more customizable solution that I'm being lazy about and not removing):
#ifndef INC_NSENUM_HPP#define INC_NSENUM_HPP#define NSENUM_OPERATORS(enum_name)                         \ 	inline operator internal_type() const {             \ 		return m_value;                             \ 	}                                                   \ 	                                                    \ 	inline bool operator==(internal_type value) const { \ 		return m_value == value;                    \ 	}                                                   \ 	inline bool operator!=(internal_type value) const { \ 		return m_value != value;                    \ 	}#define NSENUM_CONSTRUCTORS(enum_name)                           \ 	inline enum_name() {                                     \ 	}                                                        \ 	inline enum_name(internal_type value) : m_value(value) { \ 	}#define NSENUM_BEGIN(enum_name)      \ class enum_name {                    \ 	public:                      \ 		enum internal_type {#define NSENUM_END(enum_name)                  \ 		};                             \ 	private:                               \ 		internal_type m_value;         \ 	public:                                \ 		NSENUM_OPERATORS(enum_name)    \ 		NSENUM_CONSTRUCTORS(enum_name) \ };// If you copy/paste this, remove the spaces after the// backslashes. They're just there to make the forums happy.#endif

Used something like this:
#include "nsenum.hpp"NSENUM_BEGIN(stuff)	alpha,	beta,	gammaNSENUM_END(stuff)	

This topic is closed to new replies.

Advertisement