c++c++buildervcldbexpress

TSQLQuery, TFields and operator []


I have a query, using TSQLQuery that goes like this

TSQLQuery* tq = new TSQLQuery(NULL);
tq->SQLConnection = atdbDM->SQLConnection1;
tq->SQL->Add("SELECT LAST_INSERT_ID();");
tq->Open();
int insert_id = tq->Fields->operator [](0)->AsInteger;

The expression

int insert_id = tq->Fields->operator [](0)->AsInteger;

is pretty clunky. Looking at the implementation, the operator[] is overloaded in the header:

public:
    TField* operator[](int Index) { return Fields[Index]; }

However, if I call:

int insert_id = tq->Fields[0]->AsInteger;

I get the compiler error:

[bcc32 Error] TRegisterFreshCSBatchForm.cpp(97): E2288 Pointer to structure 
required on left side of -> or ->*
TRegisterFreshCSBatchForm::mRegisterBtnClick(TObject *)

Any ideas why the above call don't compile?? I must be missing something..


Solution

  • The correct syntax is

    int insert_id = (*tq->Fields)[0]->AsInteger;
    

    There must be a class object, not a pointer, involved for an overloaded operator to kick in.