2017-04-13 82 views
0

我有一個查詢,使用TSQLQuery即是這樣的TSQLQuery,中tfields和操作符[]

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; 

表達

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

是相當笨重。縱觀實施,運營商[]在頭載:

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

但是,如果我叫:

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

我得到的編譯器錯誤:

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

任何想法爲什麼上面的調用不編譯?我必須失去了一些東西..

回答

2

正確的語法是

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

必須有一個類的對象,而不是一個指針,爲參與重載運營商踢。

+0

另一種方法是簡單地不要使用'TFields :: operator []',而是直接使用'TFields :: Fields []'屬性:'int insert_id = tq-> Fields-> Fields [0] - > AsInteger;'TFields: :除了一個額外的'CALL'指令,operator []'並沒有真正獲得任何東西。它存在於C++中的唯一原因是在訪問'default'屬性時提供更接近Delphi語法的C++語法。 –