Abstract identifiers...

Hi, I'm pretty new to C++, hence why I'm posting in the beginners' section.

This may seem like a stupid or obvious question, but is there any way of manipulating class variables based on the end users' input? For example, you have a class called 'person', and you want the user to be able to name that class however they wish when it is created. A way I've found of doing it (though in a way forbidden by ISO standards):

1
2
3
4
5
void initPerson()
{
    std::cin >> pInput; // where pInput is a string variable that the constructor will use for the object name
    person $pInput; // create an object with the name enclosed in the pInput variable
}


Basically I want the user to be able to specify in a non-complicated way what objects and their variables they want to work with, and from there modify them; but in a way that's compliant with ISO standards (apparently the dollar sign is not). The purpose is so I don't have to hardcode everything in that vein.

Sorry if any of this is confusing.
Last edited on
No. Identifiers must exist at compile time. That's one of the differences between interpreted and compiled languages.
There's something similar you can do, though:
1
2
3
std::map<std::string,Class> objects;
std::string str=/*...*/;
objects[str]=Class(/*...*/);


A map (also called "dictionary") is an associative structure. What it does is associate a unique key to a value (in this case, an std::string to a Class). One of the most common uses of maps is storing variables created at run time.
Last edited on
Ah, this looks promising. Thanks for you help, Helios.
Topic archived. No new replies allowed.