Skip to main content
gamedev.net
🔒 Locked

# in C++

Started by Crispy 12 replies 500+ views
Original Post
Crispy
Crispy
Hi, Is it possible to output code similarly to this C# code in C++:
  
//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());
  
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
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Oluseyi
Oluseyi
Sorry, C++ doesn''t support reflection.
sjelkjd
sjelkjd
Herb sutter wrote an interesting article about keeping string constants and enums in sync, i believe it''s in more exceptional c++.

It supported doing what you want, but anti macro purists would probably scream.
dalleboy
dalleboy
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;
Arild Fines
Arild Fines
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)
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Oluseyi
Oluseyi
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++).
Gladiator
Gladiator
quote:

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++).



RTTI


-G|aD-
Oluseyi
Oluseyi
Oops. Doesn''t invalidate my observations on reflection, though. In particular, RTTI doesn''t provide access to the constant symbols an enumeration defines, only to the alias for the enumeration.
Arild Fines
Arild Fines
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)
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Crispy
Crispy
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 #define TOSTR(a) #a in C++, so I managed to de-psych him partially, only, but... a promise is a promise

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Oluseyi
Oluseyi
quote:
Original post by Arild Fines
I know - thats why I said "An enum in C# is a first-class object".

I''m sorry, I didn''t see that. Disregard my statements.
Fruny
Fruny

  
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 ]
"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
JonStelly
JonStelly
Technically the Enum class is part of the .NET Framework, not of C#. So support to do what you''re trying exists in any .NET enabled language, including C++ (managed).
tsuraan
tsuraan
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

//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.