Dynamic variable name. plz help

C

cpp

I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's value.

This is killing me.
thx alot
 
R

red floyd

cpp said:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's value.

This is killing me.
thx alot

Why not use std::map<std::string, double>?

#include <iostream>
#include <string>
#include <map>
int main()
{
std::map<std::string, double> mymap;
std::string s("whatever");

mymap[std::string("whatever")] = 4;
std::cout << mymap << std::endl;

return 0;
}
 
P

Patrik Stellmann

cpp said:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's value.
I'm pretty sure this is not possible the way you're thinking of since
the value of s is known at runtime (or might change during runtime)
while the variable name needs to be known at compile time. However if
you just try to have a variable name and string with the same identifier
a macro might help passing only one identifier to but creating a string
with that value and a variable with that name:

#define MyMacro(Identifier) \
string s = #Identifier; \
double Identifier = 4;

so the code
MyMacro(whatever)
would do exactly what you typed above.
 
D

Default User

cpp said:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's value.

This is killing me.


Good news! You can stop worrying. You can't do it in general. There may
be platform specific ways to do symbol lookup (assuming things were even
compiled to have symbols) but that's highly unportable.

Why don't you tell us what you are actually trying do, rather than
describing your failed solution to the problem?





Brian Rodenborn
 
J

Jonathan Turkanis

cpp said:
I am trying to find out how to assign the name of a variable (aka
identifier) given a string for this name. For example:

string s = "whatever";

How can I obtain the following expression:
double whatever = 4;

As you can see, I want the variable name to be taken from a string's
value.

I think your trying to do something that would be very natural in a
scriptoing language like perl or javascript . In C++ there are
typically adequate (and usually more efficient) ways to do the same
thing, once you learn the idioms.

What problem are you trying to solve?

Jonathan
 
C

cpp

I'm pretty sure this is not possible the way you're thinking of since
the value of s is known at runtime (or might change during runtime)
while the variable name needs to be known at compile time. However if
you just try to have a variable name and string with the same
identifier a macro might help passing only one identifier to but
creating a string with that value and a variable with that name:

#define MyMacro(Identifier) \
string s = #Identifier; \
double Identifier = 4;

so the code
MyMacro(whatever)
would do exactly what you typed above.

This gets close to what I want, however not quite. But Is it possible to
make a macro that accepts the argument in this way?:

string s = "whatever";
MyMacro(s) // This macro should do this: double whatever = 123;


The reason for this is because in my input file, I have:

Tol
g
s0

What I need to do is create doubles with the same names seen in the file
(double Tol, double g, double s0). How can this be done?

Thank you all very much.
 
R

red floyd

cpp said:
This gets close to what I want, however not quite. But Is it possible to
make a macro that accepts the argument in this way?:

string s = "whatever";
MyMacro(s) // This macro should do this: double whatever = 123;


The reason for this is because in my input file, I have:

Tol
g
s0

What I need to do is create doubles with the same names seen in the file
(double Tol, double g, double s0). How can this be done?

Thank you all very much.

Use a std::map<std::string, double>. See my earlier post.
 
J

Jakob Bieling

cpp said:
This gets close to what I want, however not quite. But Is it possible to
make a macro that accepts the argument in this way?:

string s = "whatever";
MyMacro(s) // This macro should do this: double whatever = 123;


The reason for this is because in my input file, I have:

Tol
g
s0

What I need to do is create doubles with the same names seen in the file
(double Tol, double g, double s0). How can this be done?


Why must this be done!? In your compiled executable, you do not have any
variable names anymore anyway. It's all just addresses and numbers.
 
P

Patrik Stellmann

This gets close to what I want, however not quite. But Is it possible to
make a macro that accepts the argument in this way?:

string s = "whatever";
MyMacro(s) // This macro should do this: double whatever = 123;


The reason for this is because in my input file, I have:

Tol
g
s0

What I need to do is create doubles with the same names seen in the file
(double Tol, double g, double s0). How can this be done?

Thank you all very much.

That's simply impossible in c++. But let's imagine it would be possible
and you have now such variables in your code. What would it be good for?
At runtime - when you have loaded that file - you can hardly modify cour
code and add something to use those variables while at compiletime you
could add such code but don't know the names of those variables!
Maybe you should tell us little more *why* you think you need that
because most likely there's your mistake. I'd guess the use of std::map
- as already suggested by others - will be the right way in your case...
 
C

cpp

@twister.nyroc.rr.com:

I used <map>, it works perfectly. I didn't know this was a better way to go
because, quite simply, I'm a novice.

Thanks a ton, you guys have been extremely helpful :)

now to get back to my NewtonsMethod prog.
 

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

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top