Re: Dialog Class "A" Accessing Data In Derived Dialog Class "B", "C",
& "D"
On Aug 10, 6:16 pm, "JCO" <some...@somewhere.com> wrote:
Using VS 2008; C++, MFC
The title tells most of the story. I will only talk abut Class A & B i=
n
this example since Class C & D are accessed the same way. I created a
pointer of type B in Class A (Tab1). In Class B, things are done and
private member variables are set. I provided public Set() and Get()
functions in all classes to access private data variables.
In Class A, the code is as shown:
Tab1 = (CMyTab1*) new CMyTab1;
CString str1( Tab1->GetEdit1() );
CString str2( Tab1->GetEdit2() );
int n = Tab1->GetNumber();
CString strMsg( _T("") );
strMsg.Format( _T("Edit1: %s, Edit2: %s, Integer: %d"), s=
tr1, str1, n );
MessageBox( strMsg );
if( Tab1 ) =
//goes in destructor
{
delete Tab1;
Tab1 = NULL;
}
What appears to be happening... when I use the "new" operator, class B is
instantiated. In this process, the constructor of class B zeros the da=
ta
(as it should). Therefore, Class A gets empty data as illustrated from=
the
MessageBox().
Maybe the "new" operator is not the right way to go. If not, how do I
initialize the pointer in Class A (Tab1) to point to Class B?
Thanks in advance.
"new CMyTab1" creates a new CMyTab1 object, but it does not create a
tab window and so it also does not create Edit1 and Edit2 controls.
I'm guessing that you want a pointer to some CMyTab1 object that
already exists as part of the dialog. For such cases "new" is
definitely not the way to do it.
The best approach is not to use a pointer at all. Instead, use the
'Add Variable' command (right click on the tab control in the resource
editor). That can give you a CMyTab1 m_tab1 member variable in the
dialog. The wizard adds a line of code that calls DDX_Control that
"attaches" the existing control to your member variable. Then you
will be able to call things like m_tab1.GetEdit1().