-
April 26th, 2012, 10:44 AM
#1
How to access a member variables from another class?
I have an sdi program with two dialog boxes with its respectively classes (CDialog1 and CDialog2). Through a bottom in the 1st dialog box I call the second dialog box. In the second dialog box, the user gives the values for some variables. I know that I can obtain the values of the second dialog box from the first dialog box using m_variable=dlg.m_variable in the OnSecondDialog function.
I want to write in two files the variables given by the user in both dialog boxes using the serialize function on my CApplicationDoc file. My question is: how can I access the member variables of CDialog1 and CDialog2 from the CApplicationDoc class?
-
April 26th, 2012, 12:17 PM
#2
Re: How to access a member variables from another class?
Originally Posted by laephy
My question is: how can I access the member variables of CDialog1 and CDialog2 from the CApplicationDoc class?
Well, the first dialog has to set this variable to the document instance, and the second one - get this variable.
To achieve it both dialogs need to have a pointer (or reference) to the document instance.
Last edited by VictorN; May 10th, 2012 at 11:20 AM.
Victor Nijegorodov
-
May 10th, 2012, 10:44 AM
#3
Re: How to access a member variables from another class?
Originally Posted by VictorN
DO achieve it both dialogs need to have a pointer (or reference) to the document instance.
I am newbie on visual c++ and I have a dummy question. Where is better to define the pointer in the document class CApplicationDoc.h:
CDialog1::*pointer
or in the Dialog class CDialog.h:
CApplicationDoc::*pointer ?
Last edited by laephy; May 10th, 2012 at 10:51 AM.
-
May 10th, 2012, 11:30 AM
#4
Re: How to access a member variables from another class?
Originally Posted by laephy
CDialog1::*pointer
or in the Dialog class CDialog.h:
CApplicationDoc::*pointer ?
I have no ideas what CDialog1::*pointer is, nor have any clue about CDialog.h.
Usually, if you have two dialogs CDialog1 and CDialog2 that need to access the CApplicationDoc class you declare a variable in each dialog:
Code:
CApplicationDoc* m_pDoc;
, set it to NULL in ctors, and implement method to set this document pointer in each dialog:
Code:
void SetDocument(CApplicationDoc* pAppDoc) {m_pDoc = pAppDoc;}
Then you call this SetDocument for each dialog before calling corresponding DoModal or Create...
Now you'll be able to access public CApplicationDoc members/methods from within your dialogs.
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|