Skip to main content
GameDev.net gamedev.net
🔒 Locked

Generated code and naming variables

Started by Programmer K Jun 13, 2009 at 9:03 PM 2 replies 650+ views
Original Post
Programmer K
Programmer K
This should be an easy question for most experienced programmers, but I have no idea how this would be done. I have an object (container_obj) that will create new objects (new_obj) that the first object will contain. When the container_obj creates a new object, I want it to be able to give the new_obj a name. So when the new_obj is declared, it is done so with the generated name. Is this possible? If not, what other options should I pursue aside from having a "string newObjName" variable? Thank you...
/* DK */
Zakwayda
Zakwayda
What language? Also, could you describe in a little more detail what you're wanting to do? (Or perhaps give an example...)
KulSeran
KulSeran
If you are describing what I think you are describing...
and if you are talking C++...

If you want to associate some name X with some object Y try a std::map< std::string, new_obj_type >
Objects in a map are associated with a key ( in this case a string ), and can be retrieved via map::find( key );
So you can have
mymap.insert( std::pair<std::string, new_obj_type> ( "foo1", new new_obj_type ) );std::map< std::string, new_obj_type >::iterator itr = mymap.find( "foo1" );if ( itr != mymap.end() ){itr->doSomething();}

If you are talking about being able to write code like:

container_obj.make_new( "bar" );
bar.doSomething();

that isn't possible. in c++.
Programmer K
Programmer K
Yes I'm using C++, sorry.

KulSeran, your second example is what I was talking about. So I guess I'll just stick to having a string name.

Thanks all.
/* DK */

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.