473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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_MyContro l).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;
}
};

Mar 16 '06 #1
4 1407
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
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_MyContro l).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
Mar 17 '06 #2

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uE**************@TK2MSFTNGP09.phx.gbl...
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
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_MyContro l).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?


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.
Mar 17 '06 #3
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:u4**************@TK2MSFTNGP11.phx.gbl...

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uE**************@TK2MSFTNGP09.phx.gbl...
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
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_MyContro l).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?


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
Mar 17 '06 #4

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:%2***************@TK2MSFTNGP10.phx.gbl...
"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:u4**************@TK2MSFTNGP11.phx.gbl...

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.


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).

Mar 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: William Payne | last post by:
In the following code, should the code compile without casting &n to void* ? My compiler accepts it, but should it? void foo(void*) { } struct bar { int n;
5
by: trying_to_learn | last post by:
Hello All Have another qn from the practice exercises. I ve done the exercise and the birds fly member fn was called successfully. But i still cant answer the authors qn. why this ability to...
5
by: Eric Lilja | last post by:
Using a macro, can I change what type an object is being cast to? I know, the initial respone to question might be an instinctive "ugly, don't even think about it!" or "don't use macros at all",...
4
by: Abra | last post by:
I have an application where I need to send a inter-process message (a data stream) that contains among other the address of a function (member of a class). For that, I need to serialize it, so I...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
5
by: johnbrown105 | last post by:
Hello All, I did try searching the archives, but the topics seemed to be mostly about base and derived classes. I have reached Chapter 8 in Bruce Eckel's "Thinking in C++ 2nd Edition Vol....
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
3
by: braton | last post by:
Consider the following code: class A { public: void SpecialFun() {} }; class B {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.