c++ - In MFC When EndDialog() is called, when does class destructor fire? -
in mfc, typical dialog window, when onok() called mfc, function calls enddialog() function, , somehow class destructor called @ point.
suppose have public variable, string named "test", in cdialog's class, , in dialog ok button's onbnclick() event, set "test" variable value. declare instance of dialog, , call domodal() main window's class. can read variable set once domodal() returns, no problem.
void dialog1::onbnclickedok() { test = "the test string has been set."; onok(); } void cmainframe::onedittest() { dialog1 dlg; dlg.domodal(); messagebox(dlg.test, l"main frm",0); }
this works, if have dialog several fields, , variable each field. how can sure can read of values variables before destructor called? checked msdn, , understanding onok() function calls enddialog(), , @ point, after enddialog(), class destructor called. want able read values variables set onbnclick() event, don't know when mfc calls class destructor. know when destructor called once enddialog() fires?
thanks, blitz
when dlg.domodal()
returns, window handles destroyed.
the destructor called later when dlg
goes out of scope. part same c++
class.
in example, dlg
goes out of scope when onedittest()
returns. don't have worry , compiler let know if make mistake.
for testing, can add brackets , force dlg
go out of scope sooner:
void cmainframe::onedittest() { { dialog1 dlg; dlg.domodal(); messagebox(dlg.test, l"main frm",0); } //dlg goes out of scope here , ~dlg() called //next line not compile: messagebox(dlg.test, l"main frm",0); }
as side note, have more careful if had declared dlg
pointer. because have delete
pointer manually, , pointer still accessible after dlg()
destroyed. that's not case here.
Comments
Post a Comment