mutual class include problem: accessing members from one class from inside another 
Author Message
 mutual class include problem: accessing members from one class from inside another

I have the following problem:

I want to be in my view class everything concerning menus, dialog boxes,...

I have another class, CClient, which contains network code

then there's a third class, CWorld, which contains openGL code

In my view class I have a CWorld member m_world and a CClient member
m_client

when I choose a certain menu, I need to acces client code, and this works
fine

now when a new users connects (this happens in CClient code) I want to
display a message for a certain time
to do this, I start a timer in my View and call a function of CWorld to draw
it using openGL

what is a proper way to do this?

because now to be able to do this i have to declare a CTestView* member
m_view in CClient which is fill in when I construct m_client:
 in CTestView::CTestView() :
{
...
m_client = new CClient(this);
...

Quote:
}

and then in CClient::NewUser()
{
    ...
    m_view->DisplayMessage(str);
    ...

Quote:
}

but I was told this is not a good idea...

so how it this done correctly?

tnx
Steven



Sat, 15 Oct 2005 18:48:19 GMT  
 mutual class include problem: accessing members from one class from inside another
This was already discussed.

What I would do is have a view member that looked like this:

void CMyView::ShowText(CString s)
   {
    c_Alert.SetWindowText(s);
    SetTimer(IDT_CLEAR_MESSAGE, 5000, NULL);
   }

void CMyView::OnTimer(UINT nIdTimer)
   {
    switch(nIdTimer)
       { /* nIDTimer */
        case IDT_CLEAR_MESSAGE:
             c_Alert.SetWindowText(_T(""));
             KillTimer(IDT_CLEAR_MESSAGE);
             break;
        ...other times here
       } /* nIDTimer */
  ...
   }

What I am most likely to do is provide a CView * pointer in my associated classes, and use
SendMessage or PostMessage (depending on my threading situation) to send the message to
the view, which would handle the display, e.g.,

LRESULT CMyView::OnShowText(WPARAM wParam, LPARAM)
    {
     CString * s = (CString *)wParam;
     ShowText(*s);
     return 0;
    }

void CClient::ShowText(CString s)
   {
    view->SendMessage(UWM_SHOW_TEXT, (WPARAM)&s);
   }

                                joe

Quote:

>I have the following problem:

>I want to be in my view class everything concerning menus, dialog boxes,...

>I have another class, CClient, which contains network code

>then there's a third class, CWorld, which contains openGL code

>In my view class I have a CWorld member m_world and a CClient member
>m_client

>when I choose a certain menu, I need to acces client code, and this works
>fine

>now when a new users connects (this happens in CClient code) I want to
>display a message for a certain time
>to do this, I start a timer in my View and call a function of CWorld to draw
>it using openGL

>what is a proper way to do this?

>because now to be able to do this i have to declare a CTestView* member
>m_view in CClient which is fill in when I construct m_client:
> in CTestView::CTestView() :
>{
>...
>m_client = new CClient(this);
>...
>}

>and then in CClient::NewUser()
>{
>    ...
>    m_view->DisplayMessage(str);
>    ...
>}

>but I was told this is not a good idea...

>so how it this done correctly?

>tnx
>Steven

Joseph M. Newcomer [MVP]

Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Sun, 16 Oct 2005 06:10:04 GMT  
 mutual class include problem: accessing members from one class from inside another

Quote:
> What I would do is have a view member that looked like this:

> void CMyView::ShowText(CString s)
>    {
>     c_Alert.SetWindowText(s);
>     SetTimer(IDT_CLEAR_MESSAGE, 5000, NULL);
>    }

> void CMyView::OnTimer(UINT nIdTimer)
>    {
>     switch(nIdTimer)
>        { /* nIDTimer */
>         case IDT_CLEAR_MESSAGE:
>              c_Alert.SetWindowText(_T(""));
>              KillTimer(IDT_CLEAR_MESSAGE);
>              break;
>         ...other times here
>        } /* nIDTimer */
>   ...
>    }

this works fine when I start the timer from within the same class (eg as a
reaction to OnKeyDown)
but fails when I call it from another class (ASSERT(m_Hwnd)) in
CWnd::SetTimer makes it crash

- Show quoted text -

Quote:
> What I am most likely to do is provide a CView * pointer in my associated
classes, and use
> SendMessage or PostMessage (depending on my threading situation) to send
the message to
> the view, which would handle the display, e.g.,

> LRESULT CMyView::OnShowText(WPARAM wParam, LPARAM)
>     {
>      CString * s = (CString *)wParam;
>      ShowText(*s);
>      return 0;
>     }

> void CClient::ShowText(CString s)
>    {
>     view->SendMessage(UWM_SHOW_TEXT, (WPARAM)&s);
>    }

same problem here: whenever I call   view->SendMessage(UWM_SHOW_TEXT,
(WPARAM)&s);
 it crashes (also ASSERT(m_hWnd))

how to solve it easily?



Sun, 16 Oct 2005 09:27:07 GMT  
 mutual class include problem: accessing members from one class from inside another
Problem solved!

very dumb mistake of me :)

my CPCClientView* member wasnt static in CClient! So it pointed to an
illegal instance



Sun, 16 Oct 2005 09:40:47 GMT  
 mutual class include problem: accessing members from one class from inside another
Using static class members is almost certainly a mistake. You shouldn't be calling this
from "another class".
                                joe

Quote:

>Problem solved!

>very dumb mistake of me :)

>my CPCClientView* member wasnt static in CClient! So it pointed to an
>illegal instance

Joseph M. Newcomer [MVP]

Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Tue, 18 Oct 2005 08:40:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. PropertySheet and member variables of one Class visible for other classes

2. Accessing COM class members when passed in to a COM class

3. How to access a member variable of a class from other class

4. form class accessing view class members?

5. Problem in Multithread access the Class member variable?

6. static member variable inside class

7. Why Only One Member Dialog Class?

8. Embed template class pointer inside managed class

9. Using a class as a template argument inside the definition of the class

10. Class inside DLL: Inherit from a class in an application

11. Using the class in the std::map inside the definition of class itself

12. mutual include problem

 

 
Powered by phpBB® Forum Software