I need to store an array of object of custom type. I wonder what would be the benefit of storing them in
OwnedArray instead of in a
std::vector? What are the use cases that
OwnedArray is the top choice?
I can store my custom type as:
I am aware that the
OwnedArray stores a list of pointer and will take care of deleting them in its destructor. But it’s unclear to me when is it more sensible to use it instead of the typical
T to be copyable or moveable whereas
OwnedArray<T> has no constraints on
std::vector<T> is more comparable to
juce::OwnedArray<T> is more comparable to
Which is best really depends on your use case. If you want to store copyable or moveable objects, go with
juce::Array), if you want to store objects that aren’t copyable or moveable,
juce::OwnedArray is the way to go.
You wouldn’t want to do
OwnedArray<MyType*> because it already stores pointers and you need to give it pointers to heap allocated objects.
That is, the usage would be like :