OwnedArray / base class pointers


#1

Okay, i’m cool with having virtual destructors so that an array of pointers to a base type is able to happily delete subclassed type objects.

But i’m a little wary of using OwnedArray - for now i’m using an Array of the base type, and when removing an object i delete it then remove the pointer from the array. Logically i assume OwnedArray will be fine, but i wasn’t sure if it did some internal magic using the size of the specified type (i.e. a subclass object will have more data in it than the base object and thus be bigger). Is this not actually a problem for OwnedArray? I’d like to leave the array to do the deleting for me, but of course i’m just a little concerned that it may not like being given subclass type objects.


#2

i’m pretty sure an ownedarray only stores pointers to objects, so the size of the data isn’t an issue. Or am i misunderstanding the question?


#3

yeah, that’s what i thought. i just wanted to make absolutely sure it was safe to do - i wasn’t sure if it did any special extra hidden memory management stuff behind the scenes… but thinking about it that would be pretty bad anyway!

i was just a bit paranoid. it was a bit of a stupid question really.


#4

Yes, it’s absolutely fine to let the OwnedArray mange and delete any subclass of the base class you specify, (as long as your class has a virtual destructor, of course).


#5