-1

I have a very basic question. I am building a user interface with MFC. In one of my buttonClicks methods I create a temporary object (folderDlg), and I want the to get this object back in other buttonClicks (somewhat like saving the handles in matlab gui). Currently I create a member object (pathFolder) in my dlg class (GUI_FORM) and set its reference to the temp object. Obviously the temp object is destructed at the end of the buttoClick, and the reference of the member is lost... What is the easiest way to keep the object created for further use?

relevant code part:

class GUI_FORM : public CDialog
{
public:
    GUI_FORM(CWnd* pParent = NULL) : CDialog(GUI_FORM::IDD, pParent) // wizard code
    {    }

    CFolderPickerDialog * pathFolder;

public:
    DECLARE_MESSAGE_MAP() 
    afx_msg void OnBnClickSaveAs();
        ...
    }

void GUI_FORM::OnBnClickSaveAs()

{

    CFolderPickerDialog folderDlg;
    if (folderDlg.DoModal() == IDOK)
        AfxMessageBox(folderDlg.GetFolderPath());
    GUI_FORM::pathFolder = &folderDlg;
}
Noam
  • 1
  • 3
  • 3
    Maybe just make it a full member variable `CFolderPickerDialog pathFolder;`, and not just a pointer. – Niall Sep 14 '15 at 10:05
  • Why do you want to reuse it? For efficiency reasons? I suggerst you to avoid that; what you are doing is already the Right Thing (TM) – marom Sep 14 '15 at 10:07
  • Had you considered learning the programming language, instead of asking about the tiniest, most basic problems? See [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329), and pick at least one. – IInspectable Sep 14 '15 at 16:17

2 Answers2

0
std::unique_ptr<CFolderPickerDialog> folderDlg(new CFolderPickerDialog);
Assert(folderDlg);
if (folderDlg->DoModal() == IDOK)
    AfxMessageBox(folderDlg->GetFolderPath());
GUI_FORM::pathFolder = std::move(folderDlg);

Then, in the definition of GUI_FORM:

class GUI_FORM : public CDialog {
public:
  // ...
  std::unique_ptr<CFolderPickerDialog> pathFolder;

To access the underlying pointer to the dialog, do pathFolder.get().

in C++, you have to worry about the lifetime of your data. This creates data whose lifetime ends when the only unique_ptr holding it is destroyed.

To move it around, use std::move.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
-2

What is the easiest way to keep the object created for further use?

Change this line:

CFolderPickerDialog folderDlg;

To this:

CFolderPickerDialog& folderDlg = *(new CFolderPickerDialog());

Whether that is a good idea, is a more complicated question. But it is the simple direct answer to the question you asked.

JSF
  • 5,281
  • 1
  • 13
  • 20
  • "Put one foot in front of the other" is an answer to "how do I climb Mt. Everest?", but I wouldn't call it a *good* answer. ;) – Yakk - Adam Nevraumont Sep 14 '15 at 10:54
  • @Yakk Both literally and figuratively, YOU brought Everest into the discussion. The OP did not. Maybe the OP should have asked, "what is a robust scalable approach to keeping objects around for further use". But that wasn't the question asked, so it wasn't the question I answered. The EASIEST method is to worry about the memory leak later (or never, because the example implies the memory leak would be trivial). A robust scalable approach is less easy (likely better, but still less easy). – JSF Sep 14 '15 at 14:02
  • Leaking resources - deliberately - is not a solution to anything. No, seriously. Not even when you point out, that you are leaking resources. – IInspectable Sep 14 '15 at 16:14