Sign in to follow this  

arrays of objects

This topic is 4592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am a confused and need rescuing. I am sure that I read that it is possible to have arrays of objects, but my complier complains.... I have a class, Cobjectlist, which I wanted to contain many (in game) objects of type Cobject. I can contain a single Cobject within my Cobjectlist, but when I try to initialise Cobject *obj[10]; I get this error: Cobjectlist.cpp(15) : error C2440: '=' : cannot convert from 'class Cobject *' to 'class Cobject *[10]' There are no conversions to array types, although there are conversions to references or pointers to arrays Am I being wonky? Is there a better way to achieve this? Overload the []? Use a list instead of an array? How else do people manage their objects (ie in game objects). Any advice gratefully received....... Si

Share this post


Link to post
Share on other sites
heheheh.. i have gone through so many different ways of managing objects in my time.. however, skipping the jibberish, my latest solution seems to be working quite well.

i don't know if anyone else has used this, i came up with it myself..

but for you, i will divulge my hard-worked-for secrets!

first we have our loverly OBJECT parent class...

[CODE]class OBJECT
{
public:
OBJECT *next;
bool del;

static void handle();
static void destroy();

OBJECT() : next(0), del(0)
{}

virtual void step();
virtual void draw();
};[/CODE]

all your other classes should be children of this...

*next is the pointer to the next object
del means it should be flagged for deletion..

step and draw are just two functions all my classes have in common.
step is what the objects do every frame, and draw.. is.. well.. it draws them..

these functions need to virtual.. it lets the compiler know that it should use the child class's step() and draw() functions rather than the parent class's...

something like that anyway.

then you have your loverly handle and destroy classes.. my favorite!

[CODE]void OBJECT::handle()
{
OBJECT *ptr = head;
while( ptr != NULL )
{
ptr->step();
ptr->draw();
ptr = ptr->next;
}
}

void OBJECT::destroy()
{
if( head == NULL ) return;

OBJECT *ptr = head;
if( head->del )
{
head = head->next;
delete ptr;
ptr = head;
}

while( ptr != NULL && ptr->next != NULL )
{
if( ptr->next->del )
{
OBJECT *temp = ptr->next;
ptr->next = ptr->next->next;
delete temp;
if( ptr->next == NULL ) return;
}
ptr = ptr->next;
}
}[/CODE]

just call both of those once per game-loop/frame... and.. voila. all your objects will now move and draw themselves, without you having to work like a bitch calling 600 different functions for 600 different objects... all handled neatly in one ickle function.

ah.. actually i forgot the most important function yet..

this one makes me cry at it's sheer brilliance. i'm so clever! :p

[CODE]OBJECT *head = 0;

OBJECT *create(OBJECT *b)
{
if( head == 0 )
{
head = b;
return b;
}

OBJECT *ptr = head;
while( ptr->next != 0 ) ptr = ptr->next;
ptr->next = b;

return b;
}[/CODE]

ok, firstly we need a "head". a starting point for our list of objects.. make sure you declare that, and make sure you initialize it to NULL.

next.. the create class. creates your object, and appends it to the list of objects!

ANDDD the bestest part.. you can still add parameters to your object constructors!

lets say i have

[CODE]class BULLET : public OBJECT[/CODE]

and... constructor [CODE]BULLET(float a, float b, float c)[/CODE]

i could call it like so

[CODE]create(new BULLET(a,b,c));[/CODE]

isnt that awesome? and if for whatever reason you need to refer to the newly created BULLET, it returns a pointer for your use.

to delete an object, just set "del" to true.

Share this post


Link to post
Share on other sites
The idea is good, but what you're describing is actually a personalized version of a basic entity management system, using a linked list embedded in the object hierarchy.

Anyway, for the original topic, look into std::list and smart pointers, they could help you manage this situation easily. However, if you are not used to STL, there's some learning to do.

For your original request, to make a C array of Cobject pointers, here's the code, syntax free, I hope:

// The array
Cobject * obj[10];

// Creating objects
obj[0] = new Cobject();
obj[1] = new Cobject();

// Using objects
obj[0]->method();

// Deleting them
delete obj[0];
obj[0] = 0; // for safety

I think that's the correct syntax for managing object pointers in an array.

Share this post


Link to post
Share on other sites
Thanks for all your help..... I will look into lists, but in the mean time persil and johnB are right. I was using flawed syntax! boohoo!

Wood n trees n all that!

Share this post


Link to post
Share on other sites

This topic is 4592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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