How a TObjArray behavior like a 'real' array?

Hi,

ui@ui:~$ root -l root [0] TObjArray *a = new TObjArray() root [1] a->operator[] tab completion not implemented for this context root [1] a->operator[] Error: Symbol a is not defined in current scope (tmpfile):1: Error: Failed to evaluate a->operator[] *** Interpreter error recovered ***

I find that in the link : root.cern.ch/doc/master/classTObjArray.html it said: Use operator[] to have “real” array behaviour.
But I can not follow it.

Since a real array behavior like:

root [0] int a[5] = {1,2,3,4,5} root [1] a[0] (int)1 root [2] a[1] (int)2

Best,
Li

Hi Li,

I think a clarification is needed here.
The operator[] makes the TObjArray behave like an array (or a vector) for what concerns the access of its elements: this is what the documentation is saying. The second part of your example refers to the initialisation. This latter cannot be expressed with curly braces.

Cheers,
Danilo

[quote=“dpiparo”]Hi Li,

I think a clarification is needed here.
The operator[] makes the TObjArray behave like an array (or a vector) for what concerns the access of its elements: this is what the documentation is saying. The second part of your example refers to the initialisation. This latter cannot be expressed with curly braces.

Cheers,
Danilo[/quote]

Hi Danilo,
Sorry I don’t get your point. As a array, I think there are some behaviors like:

root [0] int a[5] = {1,2,3,4,5} //the initialization root [1] a //call a (int*)0x142e800 root [2] a[0] //visit the element by ordinal number (int)1
for vector, some behaviors like pop() push_back() provided. I think behaviors is something like method or attribute.

But for TObjecArray:

root [0] TObjArray a1[5] = {1,2,3,4,5} root [1] a1 (class TObjArray*)0x1644ec0 root [2] a1[0] (class TObjArray)23350976 root [3] a1[2] //when I want to visit the element, it dose not return the element ? (class TObjArray)23351104 root [4] a1[1:3] Warning: Illegal numerical expression 3] (tmpfile):1: (const int)3 root [5] a1.operater[] Error: Symbol a1 is not defined in current scope (tmpfile):1: Error: Failed to evaluate a1.operater[] *** Interpreter error recovered *** root [6] a1.operater[0] Error: Symbol a1 is not defined in current scope (tmpfile):1: Error: Failed to evaluate a1.operater[0] *** Interpreter error recovered ***

After the initialization , I do not know how to visit the element ,let alone how to control it like a ‘real’ array ( or vector ) with pop() or other methods.

Best,
Li

With this you are creating a C array of TObjArray objects

This is the first element, which is a TObjArray object.

This is not C++. It looks like Python :slight_smile:

TObjArray is an array of [pointers to] TObjects. It behaves similarly to std::vector<TObject*> but itself is a TObject (inherits).

root [0] a = TObjArray(10);
root [1] a[0]
(TObject *) nullptr
root [2] a[2]
(TObject *) nullptr
root [3] a[33]
Error in <TObjArray::operator[]>: index 33 out of bounds (size: 10, this: 0x10c08c530)
(TObject *) nullptr

root [4] b = std::vector<TObject*>(10);
root [5] b[0]
(std::__1::__vector_base<TObject *, std::__1::allocator<TObject *> >::value_type) nullptr

[quote=“mato”][quote]
root [0] TObjArray a1[5] = {1,2,3,4,5}
[/quote]
With this you are creating a C array of TObjArray objects

This is the first element, which is a TObjArray object.

This is not C++. It looks like Python :slight_smile:

TObjArray is an array of [pointers to] TObjects. It behaves similarly to std::vector<TObject*> but itself is a TObject (inherits).

[code]
root [0] a = TObjArray(10);
root [1] a[0]
(TObject *) nullptr
root [2] a[2]
(TObject *) nullptr
root [3] a[33]
Error in TObjArray::operator[]: index 33 out of bounds (size: 10, this: 0x10c08c530)
(TObject *) nullptr

root [4] b = std::vector<TObject*>(10);
root [5] b[0]
(std::__1::__vector_base<TObject *, std::__1::allocator<TObject *> >::value_type) nullptr
[/code][/quote]

Hi ,
Thanks a lot !
I didn’t realize it is a pointer and I find it is strange that I can not use operator * to get the value of the pointer. Again, thanks a lot for your patient.

Best,
Li