I think you are talking about "variable variable names", well, you can do something like that in PHP, but not in C++.
As NeonBlack wrote, look onto std::map, or std::tr1::unordered_map, which is not yet in C++ standard library. unordered_map (C++) - Wikipedia, the free encyclopedia (unordered_map should be faster than map)
Here you go, an example:
map - C++ Reference
And a second one, a bit bloated maybe, but should prove helpful.
Code:
#include <iostream>
#include <map>
class Database {
public:
class DbElement {
protected:
unsigned int id;
unsigned int age;
float salary;
public:
DbElement() : id(0), age(0), salary(0.0) {}
DbElement(unsigned int _id, unsigned int _age, float _salary) : id(_id), age(_age), salary(_salary) {}
~DbElement() {}
friend std::ostream& operator<<(std::ostream& os, Database::DbElement el) {
os << "Id: " << el.id << ", age: " << el.age << ", salary: " << el.salary;
return os;
}
};
protected:
std::map<std::string, DbElement> db;
public:
Database() {}
~Database() {}
// /**
// * Direct R/W access to database elements
// */
// DbElement& operator[](const std::string& key) {
// return db[key];
// }
/**
* Adds the data to database only if given key does not yet exists
* @return bool - true if the data was added, false if the key already exists
*/
bool add(const std::string& key, const DbElement& data) {
if (db.find(key) == db.end()) {
db[key] = data;
return true;
}
return false;
}
const DbElement& get(const std::string& key) const {
return db.at(key);
}
void print() const {
for (std::map<std::string, DbElement>::const_iterator i = db.begin(); i != db.end(); ++i) {
std::cout << "db[\"" << i->first << "\"] == {" << i->second << "}\n";
}
}
};
int main() {
std::string key;
Database db;
std::cout << "Please, enter a name for this entry. 'stop' will cause the program to print database and terminate.\n";
// one word, for a string with spaces and whatnot, use std::getline()
while (std::cin >> key && key != "stop") {
db.add(key, Database::DbElement(1, 34, 3200.0));
}
db.print();
return 0;
}
Be sure to check how std::map's operator[], at() and find() work, those informations are at the manual entry linked earlier.