Re: class question

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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




.



Relevant Pages

  • Re: compare two float values
    ... (either float or double) ... num = rand; ... num = atof(fgets(cNum, sizeof cNum, stdin)); ...
    (comp.lang.c)
  • Reset Occurs After Keyboard Interrupt on Motorola HC08
    ... interrupt, the chip resets itself. ... interrupt and clear interrupt masks (asm CLI). ... void lightOff(int num); ...
    (comp.arch.embedded)
  • Re: Ex 7-5
    ... int getop; ... double num; ... void clearStack; ... not make sense if the stack has been cleared so a fall-though can't ...
    (comp.lang.c)
  • Re: Ex 7-5
    ... int getop; ... double num; ... allowed me to get whatever stack values I wanted in the first ... void clearStack; ...
    (comp.lang.c)
  • Re: General rules on interface (function) design
    ... Your advices are welcome and appreciated. ... void itoa2(char *s, int num) ...
    (comp.lang.c)