How to cast a pointer of a member function? (repost)

  • Thread starter Egbert Nierop \(MVP for IIS\)
  • Start date
E

Egbert Nierop \(MVP for IIS\)

Hi,

I have a CWindowImpl derived class , see below at ***, that needs to
subclass simple controls like textboxes.

like this...: m_MyControl.SubclassWindow(GetDlgItem(IDC_MyControl).m_hWnd);

==> problem here

m_MyControl.SetMouseHover(
reinterpret_cast<CMouseHover::ONMOUSEHOVER>(&this->myMemberFunction)
);

The VC 8 Compiler will return a casting error.


What can I do to fix this?



Thanks!



***
// for Subclassing controls so we have support for OnMouseHover and
OnMouseLeave
class CMouseHover: public CWindowImpl<CMouseHover>
{
public:
CMouseHover(): m_bIsMouseOver(false) , OnMouseHover(NULL),
OnMouseLeave(NULL)
{
}

//declare event signature
typedef void (CALLBACK* ONMOUSEHOVER)(int nFlags, int ID, POINT pt);
typedef void (CALLBACK* ONMOUSELEAVE)(int ID);

void SetMouseHover(ONMOUSEHOVER ptr) throw()
{
OnMouseHover = ptr;
}
ONMOUSEHOVER OnMouseHover;
ONMOUSELEAVE OnMouseLeave;
private:

bool m_bIsMouseOver;
BEGIN_MSG_MAP(CMouseHover)
MESSAGE_HANDLER(WM_MOUSEHOVER, OnMouseHover_System)
MESSAGE_HANDLER(WM_MOUSELEAVE, OnMouseLeave_System)
MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove_System)
END_MSG_MAP()

LRESULT OnMouseHover_System(UINT, WPARAM wParam, LPARAM lParam, BOOL&)
throw()
{
if (OnMouseHover != NULL)
{
POINT pt ={GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
UINT nFlags = (UINT)wParam;
//signal our delegate
OnMouseHover(nFlags, GetDlgCtrlID(), pt);
}
return FALSE;
}

LRESULT OnMouseLeave_System(UINT, WPARAM, LPARAM, BOOL&)
{
if (OnMouseLeave != NULL)
{
TRACKMOUSEEVENT evntTrack;
evntTrack.cbSize = sizeof(TRACKMOUSEEVENT);
evntTrack.hwndTrack = m_hWnd;
evntTrack.dwHoverTime = HOVER_DEFAULT;
evntTrack.dwFlags = TME_CANCEL | TME_LEAVE | TME_HOVER;
TrackMouseEvent(&evntTrack);
m_bIsMouseOver = false;
//signal delegate
OnMouseLeave(GetDlgCtrlID());
}
return 0;
}
LRESULT OnMouseMove_System(UINT, WPARAM, LPARAM, BOOL&)
{
ATLASSERT:):IsWindow(m_hWnd));
if (!m_bIsMouseOver)
{
TRACKMOUSEEVENT evntTrack;
evntTrack.cbSize = sizeof(TRACKMOUSEEVENT);
evntTrack.hwndTrack = m_hWnd;
evntTrack.dwHoverTime = HOVER_DEFAULT;
evntTrack.dwFlags = TME_LEAVE | TME_HOVER;
if (TrackMouseEvent(&evntTrack) == TRUE)
m_bIsMouseOver = true;
}
return FALSE;
}
};
 
C

Carl Daniel [VC++ MVP]

Egbert Nierop (MVP for IIS) said:
Hi,

I have a CWindowImpl derived class , see below at ***, that needs to
subclass simple controls like textboxes.

like this...:
m_MyControl.SubclassWindow(GetDlgItem(IDC_MyControl).m_hWnd);

==> problem here

m_MyControl.SetMouseHover(
reinterpret_cast<CMouseHover::ONMOUSEHOVER>(&this->myMemberFunction)
);

What's the inheritance relationship between *this and CMouseHover at this
point?

-cd
 
E

Egbert Nierop \(MVP for IIS\)

Carl Daniel said:
What's the inheritance relationship between *this and CMouseHover at this
point?

This is a pointer to a dialog, the active dialog class.

m_MyControl (an instance of CMouseHover) is the control that has been
subclassed.


myMemberFunction is a single function (obviously from the dialog) that -all-
controls would delegate control to as soon as the mouse hovers them.

Making myMemberfunction static is not the solution.
 
C

Carl Daniel [VC++ MVP]

Egbert Nierop (MVP for IIS) said:
This is a pointer to a dialog, the active dialog class.

m_MyControl (an instance of CMouseHover) is the control that has been
subclassed.


myMemberFunction is a single function (obviously from the dialog)
that -all- controls would delegate control to as soon as the mouse hovers
them.

Making myMemberfunction static is not the solution.

You didn't directly answer my question, but I think I can infer that there's
no inheritance relationship between *this and CMouseHover. In that case,
you can't do that cast - you need to change your design. You simply can't
cast a pointer to member of class A to pointer to member of class B unless
there's an inheritance relationship between A and B.

-cd
 
E

Egbert Nierop \(MVP for IIS\)

Carl Daniel said:
You didn't directly answer my question, but I think I can infer that
there's no inheritance relationship between *this and CMouseHover. In
that case, you can't do that cast - you need to change your design. You
simply can't cast a pointer to member of class A to pointer to member of
class B unless there's an inheritance relationship between A and B.

Right.

What I was trying to do looks like how delegates in .NET work. I just
declare a typedef (signature) a method and theoretically, casting pointers
should be possible (just as GetProcAddress, sort of).

A previous post (which nobody answered) contained an article about C++ that
you could use delegates (unmanaged), but that article, contains to much
noise... (I don't understand it).
 

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

Top