Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic variable name. plz help

cpp
Guest
 
Posts: n/a
#1: Jul 22 '05
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

red floyd
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Dynamic variable name. plz help


cpp wrote:[color=blue]
> 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[/color]

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[s] << std::endl;

return 0;
}

Patrik Stellmann
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Dynamic variable name. plz help




cpp wrote:
[color=blue]
> 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.
>[/color]
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.

Default User
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Dynamic variable name. plz help


cpp wrote:[color=blue]
>
> 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.[/color]


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
Jonathan Turkanis
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Dynamic variable name. plz help



"cpp" <me@on.the.web> wrote in message
news:R0CYb.68115$n62.21355@twister.nyroc.rr.com...[color=blue]
> 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[/color]
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


Jonathan Turkanis
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Dynamic variable name. plz help



"Jonathan Turkanis" <technews@kangaroologic.com> wrote:
[color=blue]
> I think your trying to do something that would be very natural in a[/color]

Ugh -- should be 'you're'!

Jonathan


cpp
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Dynamic variable name. plz help


Patrik Stellmann <stellmann@tu-harburg.de> wrote in
news:c0v4nu$1ch2iu$1@uni-berlin.de:
[color=blue]
>
>
> cpp wrote:
>[color=green]
>> 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.
>>[/color]
> 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.
>
>[/color]

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.
red floyd
Guest
 
Posts: n/a
#8: Jul 22 '05

re: Dynamic variable name. plz help


cpp wrote:[color=blue]
> Patrik Stellmann <stellmann@tu-harburg.de> wrote in
> news:c0v4nu$1ch2iu$1@uni-berlin.de:
>
>[color=green]
>>
>>cpp wrote:
>>
>>[color=darkred]
>>>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.
>>>[/color]
>>
>>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.
>>
>>[/color]
>
>
> 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.[/color]

Use a std::map<std::string, double>. See my earlier post.
Jakob Bieling
Guest
 
Posts: n/a
#9: Jul 22 '05

re: Dynamic variable name. plz help


"cpp" <me@on.the.web> wrote in message
news:E_bZb.30277$um1.23571@twister.nyroc.rr.com...[color=blue]
> Patrik Stellmann <stellmann@tu-harburg.de> wrote in
> news:c0v4nu$1ch2iu$1@uni-berlin.de:
>[color=green]
> >
> >
> > cpp wrote:
> >[color=darkred]
> >> 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.
> >>[/color]
> > 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.
> >
> >[/color]
>
> 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?[/color]


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

(replace y with x if you want to reply by e-mail)


Patrik Stellmann
Guest
 
Posts: n/a
#10: Jul 22 '05

re: Dynamic variable name. plz help


> This gets close to what I want, however not quite. But Is it possible to[color=blue]
> 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.[/color]

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

cpp
Guest
 
Posts: n/a
#11: Jul 22 '05

re: Dynamic variable name. plz help


cpp <me@on.the.web> wrote in news:E_bZb.30277$um1.23571
@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.
Closed Thread