variable variables

This is a discussion on variable variables within the C++ Programming forums, part of the General Programming Boards category; PHP has variable variables - which allow you to creat variables on the fly. I was wondering is C++ coudl ...

  1. #1
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128

    variable variables

    PHP has variable variables - which allow you to creat variables on the fly. I was wondering is C++ coudl do a similar thing?

    For example, if i get a user to input their name, "John". This is saved in the variable UserName.

    I then want to create an object of CreateUser, and their name IS the object.
    ie. CreateUser John;

    But this requires me to have previously set the name for the new object. "CreateUser UserName" is how it should be, but how am i able to access the value of UserName, so that an object called "John" is created and not called "UserName"?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You can use a map -

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    
    int main() {
    	
    	map<string,string> stringVars;
    
    	string varName;
    
    	cout << "Enter Variable Name: ";
    	cin >> varName;
    
    	string var;
    
    	cout << "Enter Variable: ";
    
    	cin >> var;
    
    	stringVars[varName]=var;
    
    	string temp;
    
    	cout << "Enter variable name: ";
    	cin >> temp;
    	cout << stringVars[temp] << endl;
    
    	return 0;
    }

  3. #3
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    ok i see how that works.

    but now if i want to create an object of a class from this, how to i do it?

    "className stringVars[varName];"

    this doesnt seem to work out.

    any ideas?

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Erm... Nope.

  5. #5
    fry
    fry is offline
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    i'm hoping its possible!

    variable variables are one of the best hidden features of php!

    the way in which c++ variables are declared are different, and so i dont think variable variables would exists as they do in php.
    however im sure there is some way to get a similar effect, even with a lot more code....

    btw, im only a beginner in c++ and im just trying to work it all out

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    Originally posted by fry
    ok i see how that works.

    but now if i want to create an object of a class from this, how to i do it?

    "className stringVars[varName];"

    this doesnt seem to work out.

    any ideas?

    Use

    Code:
    map<string,className> classVars;
    
    .
    .
    .
    
    string s;
    
    cin >> s;
    
    className cn = new className ( .... );
    
    classVars[s]=cn;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  2. Swap two variables with out third variable
    By nkanthikiran in forum C Programming
    Replies: 3
    Last Post: 01-30-2005, 12:33 PM
  3. Variable Call via Another Variable's Content
    By E-Rac in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2005, 12:51 PM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. Replies: 5
    Last Post: 09-05-2002, 10:16 AM

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