It’s not clear exactly what you are trying to do here, except perhaps learning basic c++ constructs. Here is some code to get you going.
#include <iostream>
#include <string>
#include <map>
using namespace std;
class TEST
{
public:
//Constructor - sets member string to input
TEST( string input ) : _name( input )
{
}
//Destructor called when object goes out of scope
~TEST()
{
cout << "Hi from destructor" << endl;
}
//Variable stored by the class
string _name;
};
int main()
{
string inputString;
cout << "Give a object name";
cin >> inputString;
// Give name to your class instance through the constructor
TEST foo( inputString );
// Store a copy of the object "foo" in a map that can be referenced by name
map< string, TEST > userNamedObjects;
userNamedObjects.insert( { inputString, foo } );
// Access the object's data based on user input name
cout << "From map: " << userNamedObjects.at( inputString )._name << endl;
// Sanity check
cout << foo._name << endl;
// Or set it directly
foo._name = "Patrick Swayze";
cout << foo._name << endl;
// The stored object doesn't change, because it's a copy
cout << "From map: " << userNamedObjects.at( inputString )._name << endl;
return 0;
}
4
solved dynamic object creation in c++?