Variable name from another

This is a discussion on Variable name from another within the C++ Programming forums, part of the General Programming Boards category; Is it possible to declare a variable with the name of the value of another variable(a string). For example have ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    21

    Variable name from another

    Is it possible to declare a variable with the name of the value of another variable(a string). For example have a string containing the word 'abc' and then declare a variable with the value of string so in this case the variable is called abc???

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No. Such things work in scripting languages only.
    Kurt

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    If you know what the type is going to be, you could use a std::map
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    21
    Quote Originally Posted by Salem View Post
    If you know what the type is going to be, you could use a std::map
    How exatly, using a char?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use std::map<std::string, X> where X is whatever type of the variable you want it to be.

    There are actually several good examples of this on the board if you can find them in a search.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Just FYI -

    Regular variable names are only used in your high-level source code. They don't exist in the (machine language) EXE file. The machine language program keeps track of variables by keeping track of their location in memory... by keeping track of their addresses.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 08:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 02:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 02:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21