Click to See Complete Forum and Search --> : Probs With Class Inheritance and Member Variables
Mark E
April 17th, 1999, 03:39 AM
Hi There,
I am having a lot of probs understanding classes and inheritance.
My problem is that I want to define a structure in a dialog class (DlgClass1). I then want to open a modal dialog box class (DlgClass2), and be able to see my original structure. I have had no luck making the Base Class of DlgClass2, DlgClass1. And when I use code such as
DlgClass1* MyInpFileDlg3;
MyInpFileDlg3 = (DlgClass1*)AfxGetApp();
MyInpFileDlg3->Struct.inta = 0;
I usually get a GPF when I exit the program.
Is there an easier way to make the structure a
member variable of DlgClass2. When you answer please treat me like an idiot, and tell me exactly where to define what.
Thanks Very Much
- Mark
Nath
April 17th, 1999, 04:13 AM
Hi,
Sorry for this message, I can't understand well what you're searching to do...
AfxGetApp() can be used to have a pointer to CWinApp class...
So your application is a Dialog based one ???
How do you define your class DlgClass1 ??
To have a DlgClass2 that inherits from a DlgClass1 you have to declare
class DlgClass2 : public DlgClass1
But I can be wrong with you question.. Can you explain it with more simple words... with more details about what you're looking for and how you try to do it. I understand better code than pure English language.
Then I'll try to help you better.
Nath.
Mark E
April 17th, 1999, 04:39 AM
Hi Nath,
I will try and explain better.
I have a dialog based application. The 2 classes
I am using are both derived from CDialog, and have a resource attached to each CDialog class. I am also using the class wizards to create new classes.
Basically I have created a modal dialog box, with
a resource, and when you select 'search for file',
a new modal dialog box pops up to let you search for a file. I want to search for a file, and store that file name in a structure, defined in the first dialog box class, and have that updated by the second dialog box that pops up.
The application I have written is more complicated, but this is a simplified version to keep it simple.
I tried to manually change the code so that my dialog box 2 derived from the first dialog box class. But i don't undertand enough where else I have to make changes, I got error messages.
Sorry for the complicated explanation, also my terminology may not be right.
I have included patches of my code:
Class 1 declaration and structure: (MCJob.h)
/////////////////////////////////////////////////////////////////////////////
// MCJob dialog
class MCJob : public CDialog
{
// Construction
public:
MCJob(CWnd* pParent = NULL); // standard constructor
struct s_Inpfile
{
char inpfile_name[100];
} ;
struct s_Inpfile Inpfile;
// Dialog Data
..................... etc etc
};
Class 2 Declaration: (MCGetInpFile.h)
/////////////////////////////////////////////////////////////////////////////
// MCGetInpFile dialog
class MCGetInpFile : public CDialog
{
// Construction
public:
MCGetInpFile(CWnd* pParent = NULL); // standard constructor
............. etc etc
}
Then I call the modal dialog box class, and
try to access the structure from MCJob as such:
BOOL MCGetInpFile::OnInitDialog()
{
CDialog::OnInitDialog();
MCJob* MyInpFileDlg3;
MyInpFileDlg3 = (MCJob*)AfxGetApp();
m_inpfile_name = MyInpFileDlg3->TempJob.inpfile_name;
This actually works, but I get GPF when I close
the application.
Thanks for your help,
- Mark
Nath
April 17th, 1999, 05:18 AM
Hi,
I think it's better to pass the pointer to MCJob to the dialog FileFind as parent window... I'm quite sure that CWinApp and CDialog pointers are not compatible. and then you can call MCJob *Dlg3 = (MCJob*)GetParent();
instead of AfxGetApp(); .
'Cause if I remember well Dialog based applications, CWinApp is called and exists. Then in InitInstance you call Dlg.DoModal();
Is that right ? However, I can't see quickly what's your pb.
But I'll try..
In France, it's lunch time...
See you later..
Nath.
Lynx
April 17th, 1999, 05:24 AM
Try to use AfxGetMainWnd( ) instead of using AfxGetApp(). If you look at the your app's InitInstance(), you can find that MCJob class is declared and assigned to m_pMainWnd. Therefore, you need to call AfxGetMainWnd() to get the correct dialog pointer. Don't forget to cast the CWnd* to MCJob*.
Good luck.
Lynx
Mark E
April 17th, 1999, 06:00 AM
Thanks for your help Lynx and Nath,
Isn't there some easy way to make the 2 CDialog classes work more closely together. Either derive one from another, or make the variable a member of both classes.
A question on AfxGetMainWnd(), my CDialog class is called from a frame, wouldn't AfxGetMainWnd() get
a pointer to the frame instead of the CDialog class where my structure and variable is stored.
Thanks Again,
- Mark
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.