//output should be:
//
//Alpha
//Beta
//Gamma
//Delta
enum E { Alpha, Beta, Gamma, Delta };
for(unsigned i = 0; i < 4; i++)
Console.WriteLine((E)i.ToString());
🔒 Locked
# in C++
Advertisement
Original Post
Hi,
Is it possible to output code similarly to this C# code in C++:
Note that this syntax is is most likely wrong as I don''t really know C# and I don''t have access to it, but the lecturer did that at school today and I kinda started wondering if it was possible in C/C++. neither typeid nor the # (operator?) are eligible for the task, but maybe there''s another way...
Crispy
The closest you can get is something like this:
enum E { Alpha, Beta, Gamma, Delta };
const char* ToString(E e)
{
switch (e)
{
case Alpha: return "Alpha";
case Beta: return "Beta";
case Gamma: return "Gamma";
case Delta: return "Delta";
default: return "unknown";
}
}
for(unsigned i = 0; i < 4; i++)
std::cout << ToString((E)i)) << std::endl;
quote:
Original post by Oluseyi
Sorry, C++ doesn''t support reflection.
This hasn''t got anything to do with reflection. An enum in C# is a first-class object, and has such has a ToString() method which returns the name of the enumerated constant.
The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)
quote:
Original post by Arild Fines
This hasn''t got anything to do with reflection. An enum in C# is a first-class object, and has such has a ToString() method which returns the name of the enumerated constant.
In Java. Not in C++. Yes, enums are classes (in C++), but there''s no way to obtain the name of any class (in C++) which would have been a reflective property (in C++).
quote:
Original post by Oluseyi
In Java.
Java doesn''t have enums. At all. Which is a shame.
quote:
Not in C++.
I know - thats why I said "An enum in C# is a first-class object". My point was that C# does not require reflection to perform this operation. This functionality is supported directly by the
enum construct itself. Ergo - The issue of reflection is irrelevant in this matter. The world holds two classes of men -- intelligent men without religion, and religious men without intelligence. - Abu''l-Ala-Al-Ma''arri (973-1057; Syrian poet)
mmkay... too bad I told him I''d find a way by next week, as he''s a bit of a dot NET fan doesn''t know C++ all that well
Anyways - since it cannot be done using conventional C++ calls, do compilers, perhaps, have some method of inquiring compiled data (which this is), implemented? The names are stored internally, I am pretty sure of that (but not entirely - maybe they''re substituted as constants by the precompiler - comments?), so in essence there should be a way of retrieving these strings.
I''m sorry if I''m pushing the topic a bit too far here, especially since all of the replies have stated it pretty clearly that what I''m trying to do is simply not made avaliable for the programmer, but my lecturer was so psyched by the C# possibility today, I''d like to ruin his fun for the heck of it (grin). He didn''t know about the possibility of outputting variable names using the macro
Crispy
I''m sorry if I''m pushing the topic a bit too far here, especially since all of the replies have stated it pretty clearly that what I''m trying to do is simply not made avaliable for the programmer, but my lecturer was so psyched by the C# possibility today, I''d like to ruin his fun for the heck of it (grin). He didn''t know about the possibility of outputting variable names using the macro
#define TOSTR(a) #a in C++, so I managed to de-psych him partially, only, but... a promise is a promise Crispy
enum E { Alpha = 0, Beta, Gamma, Delta };
const char* const E_String[] = { "Alpha", "Beta", "Gamma", "Delta" };
for( unsigned i = 0; i < 4; ++i )
std::cout << E_String[i] << std::endl;
// And for ''individual'' enums :
std::cout << E_String[Alpha] << std::endl;
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
If all that you want to do is get the string value out of an enum, Steve Rabin wrote up a really simple way in Game Programming Gems 3. Here's how it works:
In one file, you make a list of the values you want, like so
In one file, you make a list of the values you want, like so
//data.h
DATA(Alpha)
DATA(Beta)
DATA(Gamma)
DATA(Delta)
Then, in the file that uses them, you go like this:
//implementation.cpp
#define DATA(x) x,
enum E {
#include "data.h"
};
#undef DATA
#define DATA(x) #x
const char* Estrings[] = {
#include "data.h"
};
#undef DATA
Then, for your code, you can do the same idea
for(int i = 0; i < 4; i++) {
cout << Estrings[ i ] << endl;
}
It's not as pretty as the C# way, but it's still pretty easy to use, and not terribly ugly...
EDITS: trying to get formatting nicer...
[edited by - tsuraan on October 4, 2002 8:31:33 PM]
[edited by - tsuraan on October 4, 2002 8:32:41 PM]
[edited by - tsuraan on October 4, 2002 8:33:29 PM]
[edited by - tsuraan on October 4, 2002 8:34:09 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement