ReferenceCountedArray doubts

I’m searching a way that each object knows what position is placing in an Array, having a member variable with its position in the array. I don’t understand well ReferenceCountedArray so I did this little test:

ReferenceCountedArray<EventoFrases> arraytest; arraytest.add(new EventoFrases); int test = arraytest[0]->getReferenceCount();

Where EventoFrases inherits from ReferenceCountedObject. However test is always ‘2’. I expected that it was ‘0’, because is the place that this object has in the array. It doesn’t matter how many objects I add to arraytest, getReferenceCount() is always ‘2’. I feel a little confused about it.

Could this class serve my purpose? I miss more documentation about it.

The reference count is not the index of the object in the array, it is the number of pieces of code which “own” (i.e. refer to the object).

Thanks TheVinn. One more question: what are pieces of code? I didn’t understand it.

Array’s ELEMENTS don’t store their position. Array DOES however, but in an intrinsic way, as elements follows each other in memory.
=> You can’t figure out the index of an element by looking at the element itself, but you can use the Array::indexOf method (or similar), if and only if you need to search for the element and the element is unique in the array.

if you really do want to store the index in the item, just set it to be the current size of the array before you add it.

I’d only bother doing such a thing though if:

  1. You know the order is never going to change, AND
  2. the item (or something using it) needs to be able to know its position in the array (and doesn’t have access to the array), AND
  3. the array is going to be very large, you can’t afford to keep scanning it to find the index, and you don’t want to cache the index anywhere else

If you have access to the array, and the item is already added to it, you can do this:


If you won’t have access to the array when you need the index, or you just need to be able to get the index fast, you can do this [at the point where you add it]

thing->setIndex (myArray.size()); // <- or whatever you use to store the position
myArray.add (thing);

… when the array is empty, size == 0; the first item will be at index 0, so if you store that as the index before you add the item, it will be correct.

As said, there are usually other ways of working such that you don’t need to store the index, but that doesn’t mean it doesn’t have a place.
Just remember that as soon as you remove an item from (or insert a new one at) anywhere but the very end, the contents of the array that followed it will be out of sync.

Thanks for the answers.
I just wanted a quick way to know the index of a element looking in the own element.

This is the second approach I do to this problem. I’ve few Buttons, and a Owned array with the data, related to these buttons. Each data object is asociated to a button. The idea is that final user can create, modify, and delete any button, with undo and redo.

However I find another workaround solution :slight_smile: