MFC dialog class

M

meipv

I have five dialog classes in one project. Is it possible for the data from
I store in variable m_object from one class to be passed to another dialog
class.

The basic set-up of my project. When the user in dialog class ONE clicks
OK, it goes to dialog TWO. I have a getfunciton that retrieves the data
from the variable m_object. The problem is that when I call this function
in dialog TWO it passes whatever is in my constructor.
 
D

Doug Harrison [MVP]

meipv said:
I have five dialog classes in one project. Is it possible for the data from
I store in variable m_object from one class to be passed to another dialog
class.

The basic set-up of my project. When the user in dialog class ONE clicks
OK, it goes to dialog TWO. I have a getfunciton that retrieves the data
from the variable m_object. The problem is that when I call this function
in dialog TWO it passes whatever is in my constructor.

It sounds like dialog TWO's ctor should take a reference parameter, e.g.

DlgTWO(X& x)
: m_x(x)
{
}

Above, m_x is a member variable which has type X&, X being the type of
m_object. This makes m_x an alias for m_object. For this to work, the
lifetime of x must exceed DlgTWO's use of m_x.
 
M

meipv

So, if m_object is a CString. X should also be of type CString? How do I
allow the lifetime of m_x to exceed x. Since m_object is from Dialog One is
there anything that I need to change in it or are all the changes made to
dialog TWO. Thanks so much for your help and replying.
 
D

Doug Harrison [MVP]

meipv said:
So, if m_object is a CString. X should also be of type CString? How do I
allow the lifetime of m_x to exceed x. Since m_object is from Dialog One is
there anything that I need to change in it or are all the changes made to
dialog TWO. Thanks so much for your help and replying.

Assume you have two modal dialog classes, Dlg1 and Dlg2, and you have a Dlg1
button handler as below:

void Dlg1::OnCallDlg2()
{
// More on cs below.
Dlg2 dlg2(cs);
if (dlg2.DoModal() == IDOK)
whatever;
}

The Dlg2 class looks something like this:

class Dlg2
{
public:

Dlg2(CString& cs)
: m_cs(cs)
{
}

private:

CString& m_cs;
};

Member functions in Dlg2 can modify the CString passed into its ctor through
the m_cs reference. (If you don't understand this, read up on "references".)

In this Dlg1::OnCallDlg2 line:

Dlg2 dlg2(cs);

The CString cs can be a local variable declared before dlg2, e.g.

CString cs;
cs.LoadString(IDS_WHATEVER);
Dlg2 dlg2(cs);

Or it can be a member variable of Dlg1. The important thing is that it's not
destroyed while Dlg2 is still using it, because Dlg2's m_cs reference member
is just another way of referring to cs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top