Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by Deckards Feb 6, 2005 at 11:32 AM 8 replies 16k views
Original Post
Deckards
Deckards
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
Skizz
Skizz
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
_the_phantom_
_the_phantom_
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
desertcube
desertcube
OK, the AP was me + beaten by skizz + _the_phantom_
Fruny
Fruny
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
Drew_Benton
Drew_Benton
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
Deckards
Deckards
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
Null and Void
Null and Void
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)	

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.