Enum and string // variable from string

K

Kamil Grymuza

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
 
T

Thomas Matthews

Kamil said:
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
 
V

Victor Bazarov

Kamil said:
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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Kamil said:
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::eek:stream & operator << (std::eek:stream & 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;
}
 
K

Kamil Grymuza

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
 
K

Kamil Grymuza

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
 
J

Jesper Madsen

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
 
V

Victor Bazarov

Kamil said:
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
 
J

Jesper Madsen

--
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 said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top