TObjArray operator[]

Hi Everybody,

I was trying to store my objects in a TObjArray - because of its straightfoward semantics. I mean you claim in the User Guide (page 282) that operator[] is overloaded for TObjArray class. But my simple code:
{

TCut *cut1 = new TCut();
TCut *cut2 = new TCut();
TCut *cut3 = new TCut();
TCut *cut4 = new TCut();
TCut *cut = new TCut();

TObjArray *array = new TObjArray();

array->Add(cut1);
array->Add(cut2);
array->Add(cut3);
array->Add(cut4);

for (Int_t i=0; i<=array->GetLast(); i++)
if ((cut = (TCut*)array->At(i)))// it crashes with array[i]!!
{
cout << "This is number: " << i<< endl;
cut->Print();
}

}

crashes if I use []. But it runs successfully if I use function At()… Somebody should probably remove that remark from the user guide or fix it in TObjArray class…

thank you
michal

Hi Michal,
you’re probably using the “[i]” operator on a TObjArray* (accessing the i-th entry in an array of TObjArrays), instead of a TObjArray (accessing the i-th entry in the TObjArray). This is a c++ question. You need to dereference the TObjArray: (*array)[i]. See this post where I explain it in detail.
Axel.