Krivers
Krivers

Reputation: 2036

Calling class operator directly

I'd like to ask you why do I need to call class operator directly in this situation:

void __fastcall TForm2::statusDrawPanel(TStatusBar *StatusBar, TStatusPanel *Panel,
                                        const TRect &Rect)
{
  //if (Panel == StatusBar->Panels[1]) This doesn't work for me, compiler throws E2096 Illegal structure operation
    if (Panel == StatusBar->Panels->operator [](1)) // but this is working
    {
      int i  = 0;
    }
}

I'm using Borland's C++ Builder XE2. I would also like to ask you in which situations do I need to call class operator directly.

Upvotes: 1

Views: 336

Answers (1)

Seth Carnegie
Seth Carnegie

Reputation: 75130

Panels is apparently a pointer, and when you use [] on a pointer, it treats it like a pointer to an array and tries to add the offset to the pointer to get a Panels object at the given offset, which isn't want you want.

You need to dereference the pointer, either with Panels->operator[](1) or (*StatusBar->Panels)[1] to get to the object and call operator[] on it, which is probably your desired behaviour.

Upvotes: 3

Related Questions