Naming objects

I am attempting to name objects after strings. For example:

string input;
cin>>input;

objectType input;

I have also tried:
objectType 'input'
objectType "input". Any suggestions?
You can use a map for this:
map<string,objectType> objectMap;
You access the object with the name in "input" using objectMap[input]. It will be created if it does not exist yet.

http://www.cplusplus.com/reference/stl/map/
http://www.cplusplus.com/reference/stl/map/operator%5B%5D/
This code should allow me to create a new object and allow me to access functions from that class

cin>>input //input is jack determined by user at runtime

map <input, objectType> objectMap;
//<string, objectType>or do I use the map
Then objectMap[input].newEmployee

should the user input a name like Jack James into the input would the object name become JackJames or produce an error? To put this into context I am writing a personell program and have created several classes to specify certain types of employees, at this point I simply need to figure out how to create the object names.
Youre thinking about classes way wrong, and your initial idea just plain won't work.
should the user input a name like Jack James into the input would the object name become JackJames or produce an error?

Neither. operator>> reads until the first whitespace, so it would be just "Jack". Use getline instead:
getline(cin,input);
ResidentBiscuit wrote:

Youre thinking about classes way wrong, and your initial idea just plain won't work.


This is not constructive advice and borders flaming. If you believe the OP has fallacious information, elaborate for him. Otherwise, don't bash people for trying to think outside the box.

Then where is my logic flawed? I have created a base class person, this class stores the first and last name, from there a derived class Employee holding certain atributes, and further a third class derived, labTech for more specific atributes. My goal is to create a bunch of these people of different classes. I've written a short main with a menu and then a switch to create an object of a specific type and modify it details, store the object into a ADT that is good efficient to search for these objects and allow me to sort by different details of the object. And finally I must be able to save all of these objects to a file and reload them later. My issue is in the naming of the objects. A brief idea of what I am trying to accomplish

have classes person, employee, lab

add new type employee chosen
cout<<"input new employee's name"<<endl;
cin> string name
after input name = Erik Smith
employee name (goal: employee ErikSmith)
ErikSmith.function() (modifies Erik's attributes)
Put Erik on Queue rinse and repeat, or different menu option
I'm sorry if I implied that you had flawed logic. What Biscuit wrote wasn't constructive, and if I wanted feedback on my post, I'd prefer something I wouldn't understand as opposed to someone simply stating I'm wrong.

As to your program idea, I would suggest looking into file input and output for storing their attributes. I would also suggest using ID numbers for reference, and just storing the name as another piece of information in that id numbers file.
The perceived flaw is the confusion between object names, a source code concept, which doesn't exist at runtime, and object values. To create an object of some type (e.g. Employee) with a given value (e.g. "Erik") you're supposed to use the value as a constructor argument.

1
2
3
4
 
getline(cin, name);
employee obj(name);
queue.push_back(obj);

Sorry if I seemed as if I was flaming, I planned on elaborating more. Not sure I why I didn't.

Anywho, you cant name variables, with a variable name itself. Not to mention, this would become a pain to debug and maintain if the program were to get very large.

Now with naming objects with the name of person it represents, this is not really necessary considering a person class will likey have a name member anyway. So, a possible constructor could take an argument with just the name, this would make more sense to me. This also allows you to find this object at runtime fairly easily.
Topic archived. No new replies allowed.