Instancing an Object with a Dynamically created name?

Started by
20 comments, last by stylin 18 years, 5 months ago
That's the easy part, I need to instance objects without doing something like:

Object a;
Object b;

8,000 times over..

EDIT: I didn't see that post above when i wrote this.
C:DosC:DosRunRunDosRun!
Advertisement
Quote:Original post by MaulingMonkey
*** Source Snippet Removed ***

Obviously, a few more layers of wrapping should be used. I'm just showing the door :-).

Edit: But, this is assuming you want to create objects - if you're just looking to look them up, see the post bellow :-p.

Yes, you could use a map, and you'd probably be generating iterative strings to create your objects - but if you're doing that, wouldn't it be simpler to use a 1-dimensional array? The index itself acts like the object's name for you.

@OP: Do you really need (possibly) hundreds of unique variable identifiers?
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
I'm suprised that no one else noticed, but your example code shows that you are trying to use pointers, but you don't know what they are. A pointer is like a variable without a name. I am getting sleepy so I can't elaborate but you independantly came up with the concept of a pointer, and should be proud. However you probably don't need to use pointers directly. I would look into a container, such as std::vector or map, as it will handle all of the memory management for you. I suggest getting yourself a good C++ book.
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.


#include

std::vector objects;

int i = 0;
while( !doomsday ) {
objects.push_back( object() );
++i;
}

objects[69].Set();
objects[420].Set();
^^ me. Using a std::vector as shown above gives you the syntax you want without all the overhead of a std::map.

EDIT: you actually don't need i if doomsday is an exterior function.
: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 stylin

@OP: Do you really need (possibly) hundreds of unique variable identifiers?


Maybe, I'm going to have the program generate a 20x20x20 matrix of objects, and I need each object to have 1 of a few colors randomly selected at generation. I also need to check the neighbors of each object.

Quote:Original post by Anonymous Poster

#include

std::vector objects;

int i = 0;
while( !doomsday ) {
objects.push_back( object() );
++i;
}

objects[69].Set();
objects[420].Set();


Unless I declare what type of data it's going to hold when I create a vector, like-

std::vector<string> objects;

The compiler gives me an error. [headshake] So how exactly do I store them in the vector?
C:DosC:DosRunRunDosRun!
Quote:Original post by Crashman
The compiler gives me an error. [headshake] So how exactly do I store them in the vector?

The anonymous post cut my angle brackets out. It should have read:
   std::vector< object > objects;

Quote:... I'm going to have the program generate a 20x20x20 matrix of objects, and I need each object to have 1 of a few colors randomly selected at generation. I also need to check the neighbors of each object.

Look into boost::multi_array, or store your objects in a 1 dimensional std::vector, something like this:
#include <vector>const unsigned int WIDTH = 20;                 // cube dimensionsconst unsigned int HEIGHT = 20;const unsigned int DEPTH = 20;// two methods to generate your objectsvoid object_generation_method1( std::vector< object > & );void object_generation_method2( std::vector< object > & );// function to retrieve an object @ location ( x,y,z ) in the cubeobject & get_object( std::vector< object > &, int x, int y, int z );int main() {   // fill array with enough objects for a [WIDTH][HEIGHT][DEPTH] cube   std::vector< object > cube( WIDTH*HEIGHT*DEPTH, object() );   object_generation_method2( cube );          // set colors   ...                                         // do something useful with the cube}void object_generation_method1( std::vector< object > & cube ) {   for( int i=0; i<WIDTH )      for( int j=0; j<HEIGHT )         for( int k=0; k<DEPTH ) {            cube[ ( i*HEIGHT + j ) * DEPTH + k ].set_color( /* random number */ );            ...                                // other useful stuff         }}void object_generation_method2( std::vector< object > & cube ) {   std::vector< object >::iterator it = cube.begin();   while( it != cube.end() ) {      (*it++).set_color( /* random number */ );      ...                                      // other useful stuff   }   return;}object & get_object( std::vector< object > & cube, int x, int y, int z ) {   return cube[ ( x*HEIGHT + y ) * DEPTH + z ];}

With the get_object function, it's trivial to check neighboring objects. Let me know if this is still unclear, and I'll try to explain a little better.

EDIT: fixed typo.

[Edited by - stylin on October 23, 2005 5:44:31 PM]
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Ok, that's working the way I had in mind, Thanks Stylin. [grin]
C:DosC:DosRunRunDosRun!
I do have one question about the iterators and that last function (two questions?). Is there a way to delete a portion of a vector without using an Iterator? and I tried making a function return an object (reference) like-

Quote:object & get_object( std::vector< object > & cube, int x, int y, int z ) {

return cube[ ( x*HEIGHT + y ) * DEPTH + z ];
}


but it threw a syntax error at me. And the reason I wanted to know a more direct way of removing/accessing vectors is because when I made a 20x20x20 cube, it bombed out on me.
C:DosC:DosRunRunDosRun!
Quote:Original post by Crashman
I do have one question about the iterators and that last function (two questions?). Is there a way to delete a portion of a vector without using an Iterator?

Ya know, you can put std::vector into a search engine and find numerous pages containing the information your looking for, that is the members, implementation and syntax of the vector class, and you'll probably get a better explaination than you would here anyway.

Quote: ... and I tried making a function return an object (reference) like-

Quote:object & get_object( std::vector< object > & cube, int x, int y, int z ) {

return cube[ ( x*HEIGHT + y ) * DEPTH + z ];
}


but it threw a syntax error at me. And the reason I wanted to know a more direct way of removing/accessing vectors is because when I made a 20x20x20 cube, it bombed out on me.

Ya know, saying things like "it threw a syntax error at me" and "it bombed out on me" doesn't give anybody *any* indication of what's going on in your code. The sooner you realise this, the better off you'll be.

Have a nice day.

This topic is closed to new replies.

Advertisement