|
-
December 29th, 2011, 02:03 PM
#1
[RESOLVED] Variable name in string to variable
I have a variable name stored in a string as:
string var = "xyz"; //xyz will be different based on fucntion call
but each of these "xyz" are indeed variables. So, I have:
xyz = 1;
abc = 13.5;
etc.
So, you can see that they can be an int or double.
How do I use the stored string in 'var' to access the variables?
Thanks
-
December 29th, 2011, 02:21 PM
#2
Re: Variable name in string to variable
-
December 29th, 2011, 02:24 PM
#3
Re: Variable name in string to variable
You could create a std::map from strings to pointers, and initialize it so that each name points to the appropriate variable.
However, additional complexity would be needed to deal with multiple variable types. The specifics would depend on why you are trying to do this.
-
December 29th, 2011, 02:41 PM
#4
Re: Variable name in string to variable
 Originally Posted by Learned
How do I use the stored string in 'var' to access the variables?
String content is a run time information. Variable name is a compile time information. When module is built, there's no information about variable names anymore. So, what is the answer to your question? It is: No way.
What is another lesson to be learned from this? It is: C++ is not like PHP.
Last edited by Igor Vartanov; December 29th, 2011 at 02:47 PM.
Best regards,
Igor
-
December 29th, 2011, 07:29 PM
#5
Re: Variable name in string to variable
 Originally Posted by Learned
I have a variable name stored in a string as:
string var = "xyz"; //xyz will be different based on fucntion call
but each of these "xyz" are indeed variables. So, I have:
xyz = 1;
abc = 13.5;
etc.
So, you can see that they can be an int or double.
How do I use the stored string in 'var' to access the variables?
Thanks
What are you trying to do? Why do you think you need this?
-
December 29th, 2011, 09:35 PM
#6
Re: Variable name in string to variable
 Originally Posted by Learned
I have a variable name stored in a string as:
string var = "xyz"; //xyz will be different based on fucntion call
but each of these "xyz" are indeed variables. So, I have:
xyz = 1;
abc = 13.5;
etc.
So, you can see that they can be an int or double.
How do I use the stored string in 'var' to access the variables?
Thanks
If you're asking your users to enter a symbolic name and a value, you should have no problems implementing this. However, since you're here asking I guess that's not what you are attempting to do.
You can probably do this sort of thing in most interpreted languages, but I can't see any way to do it in a compiled language like C++. Are you sure you're in the right forum?
Consider that the symbolic names 'xyz' and 'abc' do not physically exist at run-time. These symbolic names are nothing more than a convenience for the programmer (and thus the compiler) to keep track of arbitrary memory locations. The symbols are discarded during compilation and replaced with constant memory references.
So at run-time you'd basically be trying to compare the string "xyz" to a memory location that either holds an integer or a float (or perhaps something else entirely). But without the symbolic names it would be impossible to tell what those memory locations were originally labelled.
The only way you might achieve this sort of thing is with a lookup table, providing you know what variables you're actually dealing with to begin with. For example:
Code:
double MyLookup( const CString & var )
{
// Compare the given var with a set of known strings and
// return the corresponding variable, cast as double.
if( var == _T("xyz") ) return(( double ) xyz );
if( var == _T("abc") ) return(( double ) abc );
// An arbitrary error value.
return(( double ) ( -1 ));
}
Of course this sort of thing would only work for a finite set of known variables. If the variables are unknown at run-time, it would be impossible to implement.
Perhaps if you told us why you need to do this we may be able to offer alternatives.
-
December 30th, 2011, 11:17 AM
#7
Re: Variable name in string to variable
 Originally Posted by PCForrest
Of course this sort of thing would only work for a finite set of known variables. If the variables are unknown at run-time, it would be impossible to implement.
Technically, it could be done for an infinite set of variables, so long as they correspond to a finite set of class members.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|