OwnedArray


#1

Err, it’s owned, is it really an array?

I’m trying to insert objects from an id, at arbitrary positions - in practice 0-256 or so. But it looks like insert(255, myObject), will actually insert at the last index is size is less than 255.

So it’s a list, not really an array? In which case I’d have to, what, fill it up with void*s and then use set()?

Bruce


#2

Being owned just means that the references placed inside of it no longer have to be managed. So if I have ClassName* name = new ClassName() and insert it into an OwnedArray then I no longer have to worry about deleting the memory that variable name is taking up because as soon as it’s removed from the array, it automatically deletes it for me.

It can be used as a list by inserting objects with the add() function or as an array by using the insert() function.


#3

Well it is an array, but I deliberately didn’t make it automatically expand the array to the size needed when you call set (index, item), because it’d be making an assumption about what to fill the intervening elements with. In practice I’ve only very rarely needed to put in an item at an arbitrary position, and in those cases it makes sense to have already added the required number of null elements beforehand.

I was never 100% happy with the “owned” name, but it’s the best I can think of… ArrayOfManagedObjects would be clearer, but too much typing.


#4

I like the owned fine, it just doesn’t work the way my mental image of an array does - in that it can be reliably accessed by index. I suspect the STL is the same though.

Anyways, I filled the array with blank pointers, and (once I remembered not to clear it when I loaded my XML) it works the way I want now.

Seemed worth mentioning, since most everything else in Juce seems to work exactly the way I presume it might :smiley:

Bruce


#5

Accidental post. 

 

See this thread : 

http://www.juce.com/forum/topic/ownedarray-or-referencecountedarray-which-should-i-be-using


#6