直接调用类操作符

时间:2012-11-04 22:33:22

标签: c++ c++builder c++builder-xe2

我想问你为什么在这种情况下我需要直接调用类操作符:

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;
    }
}

我正在使用Borland的C ++ Builder XE2。我还想问你在哪些情况下我需要直接调用类操作符。

1 个答案:

答案 0 :(得分:3)

Panels显然是一个指针,当你在指针上使用[]时,它将它视为指向数组的指针并尝试将偏移量添加到指针以获得{{1}在给定偏移处的对象,这不是你想要的。

您需要取消引用指针,使用PanelsPanels->operator[](1)来访问该对象并在其上调用(*StatusBar->Panels)[1],这可能是您想要的行为。