OwnedArray addAndReturnIndex

I sometimes add objects to an OwnedArray, and need to know the position in the array it was actually added at. Doing an add () and then an indexOf () is a little inefficient.

Wrote the method below – it may be useful to add the class.

/** Appends a new object to the end of the array and returns the index position it was added at.

        Note that the this object will be deleted by the OwnedArray when it
        is removed, so be careful not to delete it somewhere else.

        Also be careful not to add the same object to the array more than once,
        as this will obviously cause deletion of dangling pointers.

        @param newObject       the new object to add to the array
        @see set, insert, addIfNotAlreadyThere, addSorted
		@returns                    the index that the object was inserted at
    */
    int addAndReturnIndex (const ObjectClass* const newObject) throw()
    {
        lock.enter();
        this->ensureAllocatedSize (numUsed + 1);
        int pos = numUsed;
		this->elements [numUsed++] = const_cast <ObjectClass*> (newObject);
        lock.exit();
		return pos;
    }

Add always adds it to the end of the list right?
So you could use array.getLast() for the object or use array.size()-1 for the index.

It does, but, is your method thread safe?

Thread 1 may add something, and then immediately after thread 2 may add something. Thread 1 may then resume execution and request the size of the array in the hopes of finding out the position of the object that it added. Instead, it will getting the position of the object added by thread 2.