(C++) Using '::' to get enum members?
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:
or, like thie:
Skizz
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
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
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
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:
Abeit unsafe I know due to collisions, but it compiled for me?
- Drew
// 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,
};
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):
Used something like this:
#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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement