CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    [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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Variable name in string to variable

    You can't

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Variable name in string to variable

    Quote 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

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Variable name in string to variable

    Quote Originally Posted by Learned View Post
    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?

  6. #6
    Join Date
    Nov 2011
    Posts
    2

    Re: Variable name in string to variable

    Quote Originally Posted by Learned View Post
    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.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Variable name in string to variable

    Quote Originally Posted by PCForrest View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured