I have a string and want to use it as struct name dynamically.
struct employee{
atributes ....
}
string name;
cin >>name;
employee "name"
and then use the named employee !
does not work
employee &name = new employee();
does not work
I have a string and want to use it as struct name dynamically.
struct employee{
atributes ....
}
string name;
cin >>name;
employee "name"
and then use the named employee !
does not work
employee &name = new employee();
does not work
This is not possible in C++. Look at the concept of associative containers to store named references to class/struct instances, for example an std::map<std::string, employee>
. This creates a 'map' class mapping string based keys to values of type employee
.
Indeed, you can't use run-time string values as variable names. You could use a map to index objects by a string or other key type:
#include <map>
std::map<std::string, employee> employees;
employees[name] = employee();