Instancing an Object with a Dynamically created name?

Started by
20 comments, last by stylin 18 years, 5 months ago
How do you instance an Object named by a variable and then call functions from it? ie. int i = 0; while(till doomsday) { Object i; ++i; } 3.Set(); 15.Set(); Something along the lines of that.
C:DosC:DosRunRunDosRun!
Advertisement
What are you trying to accomplish? There are ways of approximating what you want to do, but 99% of the time they are not the best solution to the problem.

Enigma
In most languages, the way you're talking about, you can't/don't.

The closest you generally can come [in C++] is something like this:

map <int, variable_type>             variable_list;map <int, variable_type>::iterator   variable_picker;int     i;for(i=0; i<100000; ++i){     variable_list=variable_type();}variable_picker=variable_list.find(3);if (variable_picker!=variable_list.end()){       variable_picker.Set();}variable_picker=variable_list.find(15);...


Or if the numbers are all known at compile time you could probably do some template trickery that would lead to something akin to:
variable<3>.Set();variable<15>.Set();


but you couldn't use variables in the angle-brakets, which is likely not what you need.
All I need to do is generate Instances that I can call functions from, but I don't know how many I will need in all because the circumstances change everytime the program runs. The "naming/calling an instance with a variable" was just something i thought of..
Is there a more efficient way of doing it?

-I went over my notes and found out the max number of Instances is going to be somewhere around 8000, and they're going to be created/destroyed all the time, so I need a way to track which one is supposed to be destroyed.
C:DosC:DosRunRunDosRun!
Stick them into an array/vector/list/tree/hashtable/some other data structure which can hold an arbitrary number of objects.

Can you give a more detailed description of what it is you're actually trying to do?

Enigma
Basicly its a 3 Dimensional puzzle game, i need to have the cubes destroyed once they are in a certain position and I was trying to think which would be the most efficient way of tracking their position, their "neighbors", their color, and destroying them if put into a certain pattern.
C:DosC:DosRunRunDosRun!
Ok, I found a way to track their position and check their nieghbors status, but I still need a way of instancing objects on the fly..

[Edited by - Crashman on October 22, 2005 9:34:26 PM]
C:DosC:DosRunRunDosRun!
Quote:Original post by Crashman
Ok, I found a way to track their position and check their nieghbors status, but I still need a way of creating objects on the fly..

For a real pain you could make a sextuply-linked list:
   class object {      private:         object * left_object;      // [x-1][y][z]         object * right_object;     // [x+1][y][z]         object * up_object;        // [x][y-1][z]         object * down_object;      // [x][y+1][z]         object * back_object;      // [x][y][z-1]         object * forward_object;   // [x][y][z+1]      public:         object & left() { return *left_object; }         object & right) { return *right_object; }         object & up() { return *up_object; }         object & down() { return *down_object; }         object & back() { return *back_object; }         object & forward() { return *forward_object; }      ...   };

But that's probably more trouble than it's worth. [totally] You could see what Boost::multi_array can do for you.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Quote:Original post by Crashman
How do you instance an Object named by a variable and then call functions from it?

ie.

int i = 0;
while(till doomsday)
{
Object i;
++i;
}

3.Set();
15.Set();

Something along the lines of that.


I'd use a std::map
string strName = "1";std::map<Object*> objList;objList[strName] = new Object;objList[strName]->Set();

That's just a quick example of the dirty way to do it. What you'd want to do is utilize all the functions with the map class properly. If this method interests you and you want to see it better, let me know.

This topic is closed to new replies.

Advertisement