Re: class question
- From: David Wilkinson <no-reply@xxxxxxxxxxxx>
- Date: Sun, 26 Nov 2006 05:44:55 -0500
cdg wrote:
This is the code I was trying to use. And I am not trying to pass the
static text control member this time. What would be passed, so that I could
make a call from the other class back to a function in the main dialog.
//////////////////////////////////
//main dialog
void CSetTextTestDlg::OnStart()
{
SetText text;
text.BeginText(); //***what is passed here, not static text control now
}
void CSetTextTestDlg::TextDisplay1(int num)
{
CString s;
s.Format("%d", num);
m_cText1.SetWindowText(s);
}
void CSetTextTestDlg::TextDisplay2(int num)
{
CString s;
s.Format("%d", num);
m_cText2.SetWindowText(s);
}
///////////////////////////////////
//class declaration -
class SetText
{
public:
void BeginText();
SetText();
virtual ~SetText();
private:
void Text2();
void Text1();
};
//////////////////////////////////
//other class
void SetText::BeginText()
{
Text1();
Text2();
}
void SetText::Text1()
{
int num;
num = 1234;
TextDisplay1(num); //***what is passed here, not static text control now
}
void SetText::Text2()
{
int num;
num = 5678;
TextDisplay2(num); //***what is passed here, not static text control now
}
////////////////////////////////
cdg:
You really need to study a good book on basic C++. Your problems do not have to do with MFC or Windows, but rather the way objects communicate with each other.
A common idiom is for one object to pass a pointer to itself to another object. This is done by passing "this":
void CSetTextTestDlg::OnStart()
{
SetText text;
text.BeginText(this);
}
//other class
void SetText::BeginText(CSetTextTestDlg* pDlg)
{
Text1(pDlg);
Text2(pDlg);
}
void SetText::Text1(CSetTextTestDlg* pDlg)
{
int num;
num = 1234;
pDlg->TextDisplay1(num);
}
void SetText::Text2(CSetTextTestDlg* pDlg)
{
int num;
num = 5678;
pDlg->TextDisplay2(num);
}
This is all fine while you are just learning, but I'm not sure you have the best design here. The job of the dialog is to display the information. The job of the other class is to provide the information. The other class does not need to know about the dialog; it should just provide the information.
class Other
{
public:
int GetNumber1() const;
int GetNumber2() const;
};
void CSetTextTestDlg::OnStart()
{
Other other;
TextDisplay1(other.GetNumber1());
TextDisplay2(other.GetNumber2());
}
You do not need two-way communication here.
David Wilkinson
.
- Follow-Ups:
- Re: class question
- From: cdg
- Re: class question
- References:
- class question
- From: cdg
- RE: class question
- From: Arman Sahakyan
- Re: class question
- From: cdg
- Re: class question
- From: David Wilkinson
- Re: class question
- From: cdg
- Re: class question
- From: David Wilkinson
- Re: class question
- From: cdg
- Re: class question
- From: Scott McPhillips [MVP]
- Re: class question
- From: cdg
- Re: class question
- From: Scott McPhillips [MVP]
- Re: class question
- From: cdg
- class question
- Prev by Date: Re: Help with VC6.0 Basic Concept!
- Next by Date: How about the future of VC++/MFC
- Previous by thread: Re: class question
- Next by thread: Re: class question
- Index(es):
Relevant Pages
|