The header common.h forward declares a class Test and a function receiving a member function pointer:
class Test;
void func(const Test &t, int (Test::*f)() const, int x, int y);
In the source file target.cpp, I define the function such that
#include "common.h"
void func(const Test &t, int (Test::*f)() const, int x, int y) {
std::cout << "f: " << (t.*f)() << ", x: " << x << ", y: " << y << std::endl;
}
In my main file, I define the class Test and use the function func:
class Test {
public:
int example() const { return 1; }
};
#include "common.h"
int main() {
Test t;
func(t, &Test::example, 0xaaaaaaaa, 0xbbbbbbbb);
return 0;
}
Obviously this is a bit smelly since pointers to member functions are sometimes more than a simple pointer. But the resulting behavior is a bit overwhelming: The given parameters 0xaaaaaaaa and 0xbbbbbbbb won't be passed correctly to the function. Or to be more precise, the function func interprets the given stack differently than the data is pushed on the stack by the caller. The size of f depends on whether the class is only forward declared or actually defined. The output compiled with Visual Studio 2013 is:
f: 1, x: 0, y: 2130567168
I thought, if a forward declaration is sufficient, it really doesn't matter whether there's a definition given or not.
0xaaaaaaaaand0xbbbbbbbbare not representable asint(under windows calling conventions, see MSDN). The narrowing conversion invokes undefined behaviour. Have you tried using smaller numbers? - Mankarse