Sign in to follow this  
Fl00Fy

Add Object to Array from Constructor

Recommended Posts

Fl00Fy    198

Hey

 

I'm trying to implement collision detection for a project. So far, my idea has been to add all objects that are added into a scene into an array of objects. My question is, as in the title, can I add an object to said array in its constructor? 

 

I've tried like this 

Object::Object () 
{
	pos_x = 0; pos_y = 0; velocity = 0; direction = 0;
	objectArray [numObjects] = this.Object;
        numObjects++
}

 

and this 

Object::Object () 
{
	pos_x = 0; pos_y = 0; velocity = 0; direction = 0;
	objectArray [numObjects] = this;
        numObjects++
}

 

But neither worked, and I'm having trouble figuring out exactly how I would do it properly, if it's at all possible. 

Also, objectArray is just a pointer to an object, and I've had trouble with pointers before (and probably will for a while). 

int numObjects = 0; 

Object *objectArray;

 

Any help would be appreciated. I also realise that there are probably better ways to keep track of all the objects, or to go about handling collision detection in general. So any help with this topic, or just collision detection in general would be great.

Thanks.  

Share this post


Link to post
Share on other sites
Fl00Fy    198

Thanks for the help. I was actually considered using std::vector, and now that you mention it, it would be very useful when deleting objects. The array I created wasn't part of the object, I just needed it to be modified from within the Object class. I assume that if the vector is of actual objects and not pointers, it wouldn't make sense to add objects to the array in the constructors and end up having duplicates of all objects (those created normally and those then added to the array), but simply create the objects in the array, is that correct? 

 

And thanks again for the help

Share this post


Link to post
Share on other sites
EddieV223    1839

If your vector owns the object pointers, use a unique_ptr<Object>, then cleanup happens automatically.

Edited by EddieV223

Share this post


Link to post
Share on other sites
Fl00Fy    198

Thanks. I'll definitely look into it. 

Would it be best used as a vector containing unique_ptrs to Objects, as in 

std::vector<std::unique_ptr<Object>> AllObjects; 

 

 ... possibly..... or as a dynamically-allocated array of Objects, like std::unique_ptr<Object[]>... possibly. 

 

Would I be correct in assuming the first one is better? Since it would handle individual objects that need deleted better. 

Share this post


Link to post
Share on other sites

std::vector<Object> is perfectly fine if Object isn't too large in memory (it's member-variables count towards the memory, but not non-virtual functions). If Object has less than, say, 10 member variables, I'd recommend to just do std::vector<Object>.

 

std::vector<std::unique_ptr<Object>> would be good if Object is very large (very large), or if you need to hold Objects that are in some way 'null' in the vector. But you can just do that with a member variable of Object anyway if it makes sense to the class.

 

std::unique_ptr<Object[]> wouldn't be a good idea (it might not even compile, but I'd have to check). Everything it offers, std::vector<> also offers, but std::vector is designed for the purpose of dynamic arrays and std::unique_ptr isn't.

 

To avoid any confusion, std::vector calls the destructor of Objects just like std::unique_ptr does; and both by default hold the Object in dynamic memory. Both also cleanup after themselves. But if std::vector is holding normal pointers instead of instances, then it won't clean up what the pointer points to. (std::unique_ptr is an instance that contains a pointer, which it cleans up in its destructor, so it's cleaned up).

 

std::vector<Object> = cleaned up.

std::vector<Object*> = The memory the pointers point to are not cleaned up.

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