Sign in to follow this  
Crashman

Instancing an Object with a Dynamically created name?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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[i]=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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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();

Share this post


Link to post
Share on other sites
^^ 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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 dimensions
const unsigned int HEIGHT = 20;
const unsigned int DEPTH = 20;

// two methods to generate your objects
void object_generation_method1( std::vector< object > & );
void object_generation_method2( std::vector< object > & );

// function to retrieve an object @ location ( x,y,z ) in the cube
object & 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Quote:
object & get_object( std::vector< object > & cube, int x, int y, int z ) {

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


"it threw a syntax error at me" =
1137 C:\Dev-Cpp\Test.cpp syntax error before `&' token

Not to descriptive is it [rolleyes] those were the only new lines in my code, aside from the declaration. And I meant to say the FPS bombed out, leading to the off-hand question of erasing a portion of a vector without an iterator.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crashman
Quote:
object & get_object( std::vector< object > & cube, int x, int y, int z ) {

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


"it threw a syntax error at me" =
1137 C:\Dev-Cpp\Test.cpp syntax error before `&' token

Check if you have object defined. Check if you have #included the standard header <vector>.
Quote:
Not to descriptive is it [rolleyes] those were the only new lines in my code, aside from the declaration. And I meant to say the FPS bombed out, leading to the off-hand question of erasing a portion of a vector without an iterator.

No, and I don't think that's what you want to do anyway. In this situation, you want to keep the cube's structure, but also allow some parts of the cube to be empty, right? I would make a cell class that represents a particular location in the cube. These cells will have pointers (boost::shared_ptr) to the objects that they contain.

Think about a garage: at any time a single garage can hold a single car. When that car leaves the garage, the garage becomes empty - but the garage is still there. The cells represent the garages for the storage of objects. When you need to remove an object from the cube, the cell does that for you, that is, it releases the object it was previously holding onto. You can give it a function to tell you if an object is in there or not as well. Such a class might look like:


class cell {

public:
cell(): pObject(0) {}
~cell() { delete pObject; }
...
void store( const object & ); // loads an object into the cell
void clear(); // releases object from cell
bool empty() const;
private:
object * pObject; // our object
};


[Edited by - stylin on October 26, 2005 1:12:36 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this