Calling a function from another class (dialog)

D

drummond.ian

Hello Everyone,

This problem's been causing me a lot of trouble and I'm hoping
somebody can help me out!!

I have a dialog-based MFC application in visual studio 2003. I want to
call a function called "OnPrintMsg" from my dialog class called
"MyAppDlg.h". The function needs to be called from another class called
"Client.cpp". The Client class (derived from the CSockets class) exists
as described below.

MyAppDlg.h
....
CClient myclient; //The CClient class is another class that I created.
It inherits from CSocket.
void OnPrintMsg(); // This function is in my dialog application
....
//Client.cpp
CClient::OnReceive() // This function is called from my Client Class
upon receiving information from
// the socket
....

OnPrintMsg(); //want to call function in MyApp dialog with access to
dialog member variables


In a nutshell what I want to do from the OnReceive, is like
CMyApp::OnPrintMsg(); However, when I tried this using a local instance
of the MyApp class, I was unable to access the member variable of the
actual dialog, likely because it was a local instance.

Can anyone help me? Thanks in advance!!

~ Ian
 
V

Victor Bazarov

Hello Everyone,

This problem's been causing me a lot of trouble and I'm hoping
somebody can help me out!!

I have a dialog-based MFC application in visual studio 2003. I want to
call a function called "OnPrintMsg" from my dialog class called
"MyAppDlg.h". The function needs to be called from another class
called "Client.cpp". The Client class (derived from the CSockets
class) exists as described below.

MyAppDlg.h
...
CClient myclient; //The CClient class is another class that I created.
It inherits from CSocket.
void OnPrintMsg(); // This function is in my dialog application
...
//Client.cpp
CClient::OnReceive() // This function is called from my Client Class
upon receiving information from
// the socket
...

OnPrintMsg(); //want to call function in MyApp dialog with access to
dialog member variables


In a nutshell what I want to do from the OnReceive, is like
CMyApp::OnPrintMsg(); However, when I tried this using a local
instance of the MyApp class, I was unable to access the member
variable of the actual dialog, likely because it was a local instance.

Can anyone help me? Thanks in advance!!

This falls under FAQ 5.8 (and the rest of section 5), I believe.
Most likely you're in dire need of some education how to deal with
MFC classes. Isn't there a book or, like, a hundred, about those?
Try asking in a newsgroup dedicated to MFC. There are several.

V
 
D

drummond.ian

My problem isn't rooted in MFC, the problem is simply classes. The
problem is is I have a main class that has a member variable of another
class. A function within the member variable is called, and I want to
call a function of the main class. There is nothing strictly MFC about
the problem.

Thanks anyway
 
S

Scott McPhillips [MVP]

My problem isn't rooted in MFC, the problem is simply classes. The
problem is is I have a main class that has a member variable of another
class. A function within the member variable is called, and I want to
call a function of the main class. There is nothing strictly MFC about
the problem.

Quite true. To call a function of your "main" class from outside the
class you can use a pointer to the "main" class object. When you create
or initialize the member object pass it the 'this' pointer from the main
class. Store the pointer in a member variable and use it when need to
call functions in the "main" class object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top