i have an enum in c++
enum color {
BLACK,GREEN,RED
};
In java i can receive the string value using color.BLACK.name()
will give me the String value..
Do c++ have the same behaviour or how can i convert an enum to char*.
i have an enum in c++
enum color {
BLACK,GREEN,RED
};
In java i can receive the string value using color.BLACK.name()
will give me the String value..
Do c++ have the same behaviour or how can i convert an enum to char*.
Short answer no. There's a number of "idiomatic" macro tricks that you can do to mimick the effect, though.
The reason for this is that C++ tends to make you "Pay for what you need only" (which makes it possible to write much more performant code if you design it cardefully).
Code project has an article on this, and google will turn up many others.
Not so long ago I made some trick to have enums properly displayed in QComboBox and to have definition of enum and string representations as one statement
#pragma once
#include <boost/unordered_map.hpp>
namespace enumeration
{
struct enumerator_base : boost::noncopyable
{
typedef
boost::unordered_map<int, std::wstring>
kv_storage_t;
typedef
kv_storage_t::value_type
kv_type;
kv_storage_t const & kv() const
{
return storage_;
}
LPCWSTR name(int i) const
{
kv_storage_t::const_iterator it = storage_.find(i);
if(it != storage_.end())
return it->second.c_str();
return L"empty";
}
protected:
kv_storage_t storage_;
};
template<class T>
struct enumerator;
template<class D>
struct enum_singleton : enumerator_base
{
static enumerator_base const & instance()
{
static D inst;
return inst;
}
};
}
#define QENUM_ENTRY(K, V, N) K, N storage_.insert(std::make_pair((int)K, V));
#define QBEGIN_ENUM(NAME, C) \
enum NAME \
{ \
C \
} \
}; \
} \
#define QEND_ENUM(NAME) \
}; \
namespace enumeration \
{ \
template<> \
struct enumerator<NAME>\
: enum_singleton< enumerator<NAME> >\
{ \
enumerator() \
{
//usage
/*
QBEGIN_ENUM(test_t,
QENUM_ENTRY(test_entry_1, L"number uno",
QENUM_ENTRY(test_entry_2, L"number dos",
QENUM_ENTRY(test_entry_3, L"number tres",
QEND_ENUM(test_t)))))
*/
Now you've got enumeration::enum_singleton<your_enum>::instance()
able to convert enums to strings. If you replace kv_storage_t
with boost::bimap
, you will also be able to do backward conversion.
Common base class for converter was introduced to store it in Qt object, because Qt objects couldn't be templates
There's no direct support for this in the language. In fact, it
would be difficult, if not impossible, to do for the general
case: enum
in C++ has only a limited relationship with
enumerated types, and you can write things like:
enum E
{
a = 0,
b = 0,
c = 0
};
, with several enum constants having the same value.
(I've written code which parses enum statements, and generates
mapping code to and from strings; given an enum such as the
above, it will map an enum value of 0 to "a"
, regardless of
whether it was set to a
, b
or c
. It's still incredibly
useful, but I think this is the correct solution: an external
program to generate the mappings if you need or request them.)