Passing WndProc as a method parameter (Function Pointers
-
Hi guys, I have a question about function pointer passsing. The following code is the beginnings of a Win32 app.
//////////////////////////////////////////////////// main.cpp //////////////////////////////////////////////////// #include "windows.h" #include "classOne.h" LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { //empty for code clarity return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Nothing n(WndProc); return 0; }
As you can see, all I'm trying to do is pass the WndProc function as a parameter to the constructor of a class called Nothing.//////////////////////////////////////////////////////// Nothing.h //////////////////////////////////////////////////////// #include "windows.h" class Nothing { public: Nothing(WNDPROC wndProc); };
//////////////////////////////////////////////////////// Nothing.cpp //////////////////////////////////////////////////////// #include "classOne.h" Nothing::Nothing(WNDPROC wndProc) { int kk = 0; }
Well my question is, why is it that when I step into the constructor of the "Nothing" class, wndProc appears to have a different address than the one I passed in? For the life of me I can't see why this is the case as WndProc is typedef'd as LRESULT (*WNDPROC)(HWND, UINT, WPARAM, LPARAM) I'm sure that if I go any further, I'm likely to get an error for example, when I use that address to initialize and register a window class. Any hints?? Woke up this morning...and got myself a blog -
Hi guys, I have a question about function pointer passsing. The following code is the beginnings of a Win32 app.
//////////////////////////////////////////////////// main.cpp //////////////////////////////////////////////////// #include "windows.h" #include "classOne.h" LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { //empty for code clarity return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Nothing n(WndProc); return 0; }
As you can see, all I'm trying to do is pass the WndProc function as a parameter to the constructor of a class called Nothing.//////////////////////////////////////////////////////// Nothing.h //////////////////////////////////////////////////////// #include "windows.h" class Nothing { public: Nothing(WNDPROC wndProc); };
//////////////////////////////////////////////////////// Nothing.cpp //////////////////////////////////////////////////////// #include "classOne.h" Nothing::Nothing(WNDPROC wndProc) { int kk = 0; }
Well my question is, why is it that when I step into the constructor of the "Nothing" class, wndProc appears to have a different address than the one I passed in? For the life of me I can't see why this is the case as WndProc is typedef'd as LRESULT (*WNDPROC)(HWND, UINT, WPARAM, LPARAM) I'm sure that if I go any further, I'm likely to get an error for example, when I use that address to initialize and register a window class. Any hints?? Woke up this morning...and got myself a blogWhat you are passing here is a pointer to a function. The name of a function is always it's address, like using the address-of operator automatically. This address, of course, refers to the starting point of the function in memory. However, in this case, the constructor seems to create a copy of the function, and move it into a seperate place in memory. I do not know why it does this, but calling the function does work properly. Here is the code fragment with which I tested it.
#include <iostream>
using namespace std;typedef void (*ptrFunc1)(int, int, double, double);
void MyFunction( int a, int b, double c, double d )
{
cout << "Inserted: " << a << " " << b << " " << c << " " << d << "\n\n";
}class Nothing
{
public:
Nothing(ptrFunc1 func1);
};Nothing::Nothing(ptrFunc1 func1)
{
int a = 10;
int b = 20;
double c = 30;
double d = 40;func1(a, b, c, d);
}
int main(void)
{
Nothing MyNothing(MyFunction);
}Although whilst in debugging, the addresses of MyFunction and func1 are different, and even (*func1) points to a different place than MyFunction, the code calls MyFunction properly through the func1 function pointer, and the text gets printed out correctly. I see no reason why your implementation would not work properly as well. The reasons of why the memory locations are different is unknown to me, it would require some more work to determine it. I even tested it by creating two objects of class Nothing. In that case, the func1 pointed to the same location in both cases. So, although I don't understand why it happens, the code works, so you shouldn't have anything to worry on that part :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.