By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,930 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,930 IT Pros & Developers. It's quick & easy.

Enum and string // variable from string

P: n/a
Hi
Is it possible to have a name of enumerated value in string somehow?
enum {
ID1 = 1,
ID2=2,
}

And can I somehow make something like
cout<<ID1<<endl; and heve printed "ID1" not 1?

And also is it possible to call a variable having only string with it's
name. i.e. I have a string: "variable_name" and I want to do something like
variable_name=value;

Is it possible?

Thanks very much
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Kamil Grymuza wrote:
Hi
Is it possible to have a name of enumerated value in string somehow?
enum {
ID1 = 1,
ID2=2,
}

And can I somehow make something like
cout<<ID1<<endl; and heve printed "ID1" not 1?

And also is it possible to call a variable having only string with it's
name. i.e. I have a string: "variable_name" and I want to do something like
variable_name=value;

Is it possible?

Thanks very much


A nice design is to have your enum as a class.
Give the class some methods for convert from
string to value and vice-versa.

By having the enum as a type, you could add more
control to the assignments.

Otherwise, try either a table of <string, value>
or a std::map.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #2

P: n/a
Kamil Grymuza wrote:
Is it possible to have a name of enumerated value in string somehow?
enum {
ID1 = 1,
ID2=2,
}

And can I somehow make something like
cout<<ID1<<endl; and heve printed "ID1" not 1?
#define name_of(x) #x
....
cout << name_of(ID1) << endl;
And also is it possible to call a variable having only string with it's
name. i.e. I have a string: "variable_name" and I want to do something like
variable_name=value;
I don't understand the problem you're trying to solve.
Is it possible?


As soon as you tell what "it" is.

V
Jul 23 '05 #3

P: n/a
Kamil Grymuza wrote:
Is it possible to have a name of enumerated value in string somehow?
enum {
ID1 = 1,
ID2=2,
}

And can I somehow make something like
cout<<ID1<<endl; and heve printed "ID1" not 1?


enum ID {
ID1, ID2
};

std::ostream & operator << (std::ostream & os, ID id)
{
switch (id)
{
case ID1: os << "ID1"; break;
case ID2: os << "ID2"; break;
}
return os;
}

int main ()
{
std::cout << ID1 << std::endl << ID2 << std::endl;
}
--
Salu2
Jul 23 '05 #4

P: n/a
The problem is that I have some variables I can't put in an array, they
are names of buttons, text controls etc. I obtain a name of element that
called a function and by that name I would like to call some method
of this object, and I don't know whether this is possible by some simple
cast from string (name of the variable) to some pointer or whatever, the
other idea I have is to make a switch and compare all the possibilities
but this is totally unelegant.

If it is possible to make that 'cast' could you please write me how to
do it, or maybe you have some idea how to solve the problem?

Thanks for your help
bye
Jul 23 '05 #5

P: n/a
About that enumerated values, I meant that I have an integer value of ID
and I want the name, and when I tested your suggestion with my program.
I have sth. like this:
#define name_of (x) #x

cout<<name_of(event.GetId())<<endl;

it gave me
event.GetId() on screen
so again sorry for not explaining my problem clearly, any ideas now?
Bye
Jul 23 '05 #6

P: n/a
I have done something like the following sometimes ( I haven't compiled
this, it is just from memory.. )
create a file that invokes a macro on each element, and do too files that
defines the macro differently.

Hope this is what you want...
----file: enum.inc
ITEM(ID1),
ITEM(ID2),
ITEM(ID3),
ITEM(ID4)

----file: enum_names.h
#define ITEM(x) #x
const char * names[40] = {
#include "enum.inc"
};
#undef ITEM

----file: enum_values.h
#define ITEM(x) x
enum {
#include "enum.inc"
};
#undef ITEM

--
Jesper Madsen, SAXoTECH

"The truth of the matter is that you always know the right thing to do. The
hard part is doing it"
- General H. Norman Schwartzoff
"Kamil Grymuza" <gr****@wp.pl> wrote in message
news:d6**********@nemesis.news.tpi.pl...
About that enumerated values, I meant that I have an integer value of ID
and I want the name, and when I tested your suggestion with my program.
I have sth. like this:
#define name_of (x) #x

cout<<name_of(event.GetId())<<endl;

it gave me
event.GetId() on screen
so again sorry for not explaining my problem clearly, any ideas now?
Bye

Jul 23 '05 #7

P: n/a
Kamil Grymuza wrote:
About that enumerated values, I meant that I have an integer value of ID
and I want the name, and when I tested your suggestion with my program.
I have sth. like this:
#define name_of (x) #x

cout<<name_of(event.GetId())<<endl;

it gave me
event.GetId() on screen
so again sorry for not explaining my problem clearly, any ideas now?


Not really. If you don't have duplicate IDs, then you can use Thomas'
suggestion about a 'map<ID,string>'. If you have duplicates, you can't
use that, for obvious reasons.

Whatever other thing you wrote about "names of buttons, text controls
etc.", I have absolutely no idea what you need. You're apparently trying
to solve some problem by "casting" something "from string" or vice versa,
but you didn't say what that problem was. Have you tried simply taking
the address of the variable, converting it to void* and displaying it?
Again, I have no idea if that's what you need because I don't understand
the problem you're trying to solve.

You could, of course, create a 'map<void*,string>' and then add "names"
when you create your objects, and then display those names based on the
address of the object when you feel like it. That's intrusive (requires
you to change code), and not really reliable since you can simply assign
the same "name" to different object addresses and it's not going to stop
you... Maybe that's what you need?

V
Jul 23 '05 #8

P: n/a


--
Jesper Madsen, SAXoTECH

"The truth of the matter is that you always know the right thing to do. The
hard part is doing it"
- General H. Norman Schwartzoff
"Jesper Madsen" <ba***@mail.stofanet.dk> wrote in message
news:42*********************@nntp04.dk.telia.net.. .
I have done something like the following sometimes ( I haven't compiled
this, it is just from memory.. )
create a file that invokes a macro on each element, and do too files that
defines the macro differently.

Hope this is what you want...
----file: enum.inc
ITEM(ID1),
ITEM(ID2),
ITEM(ID3),
ITEM(ID4)

----file: enum_names.h
#define ITEM(x) #x
const char * names[40] = {
#include "enum.inc"
};
#undef ITEM

----file: enum_values.h
#define ITEM(x) x
enum {
#include "enum.inc"
};
#undef ITEM

forgot to write: std::cout << "name: " << names[ event.GetID() ] << " value
:" << event.GetID() << std::endl;


--
Jesper Madsen, SAXoTECH

"The truth of the matter is that you always know the right thing to do. The hard part is doing it"
- General H. Norman Schwartzoff
"Kamil Grymuza" <gr****@wp.pl> wrote in message
news:d6**********@nemesis.news.tpi.pl...
About that enumerated values, I meant that I have an integer value of ID
and I want the name, and when I tested your suggestion with my program.
I have sth. like this:
#define name_of (x) #x

cout<<name_of(event.GetId())<<endl;

it gave me
event.GetId() on screen
so again sorry for not explaining my problem clearly, any ideas now?
Bye


Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.