Hello !
I have an MFC app ( I know this is C++ area, but it involves vectors which are C++ :) ) that has a dialog. Now the user adds input to the dialogue which is stored in a structure. In my main app, I then say something like
CMyDialog dlg;
if (dlg.DoModal() == IDOK) {
// Get the structure
MyStructure s = dlg.GetStructure();
vectorObject.push_back(s)
}
Ok. Now where I thought a problem would lie is the fact that
CMyDialog::GetStructure() is defined as
MyStructure& GetStructure() { return m_structure; } ###
where
MyStructure m_structure
is a member variable.
Surely ### is returning a reference to *a local object* hence I am adding a temporary object to vectorObject that should be destroyed as soon as dlg goes out of scope ! But this is not the case ! All structures added to vectorObject remain throughout the lifetime of the application !
I figured this was since push_back adds a copy of the object passed to it, but the dox do not say this. They also say that push_back takes a reference in itself....surely this complicates it further !?
Why is this ?
(Appologies in advance if I am having a dumb moment...)