2013-02-05 31 views
0

代碼是在這裏Qt的使用lambda功能時

void A::fun() 
{ 
    QAction* act = new QAction(this); 
    QAction* act2 = new QAction(this); 
    connect(act, QAction::triggered, [this, &act2]() {...; act2.setDisable(true);} 
           // crash when &act2 is used to capture the variable 
           // but it is okay using act2 to capture the variable 
} 

的原因是什麼連接會崩潰嗎?謝謝。

回答

3

您正在服用參考act2即使它是一個指針,將走出去的範圍,這就是爲什麼複製指針的作品。

+0

它崩潰而不是給出編譯錯誤。爲什麼超出範圍?謝謝。 – user1899020

+1

'act2'上的_object_不在範圍之外(它在堆上),但變量'act2'只在'fun()'範圍內。 – cmannett85