MyClass is a singleton class (There will only ever be one of these in my whole program).
What I开发者_如何学JAVA want to do is follows.
- Add data to my class using AddData, get a function pointer returned that I can then pass to 'another' function in a dll.
- Then this 'other' function calls my call back function
My class is like so.
typedef void (*DataReceivedCallback)(int, int);
class MyClass
{
MyClass();
~MyClass();
void AddData(int sourceId, DataReceivedCallback &callback);
static void MyCallBackFunction(int var1, int var2);
};
void MyClass::AddData(int sourceId, DataReceivedCallback &callback)
{
callback = &MyCallBackFunction;
}
void MyClass::MyCallBackFunction(int var1, int var2 )
{
//do something blah blah
}
I can then do:
int main()
{
DataReceivedCallback callback;
MyClass->GetInstance()->AddData(1, callback);
callback(1,100);
}
When I step through this I see that I do actually step into the callback MyCallBackFunction which is what I want :)
What I then want to do now is pass this 'callback' defined in main to a dll function that will call back into my callback function.
I have the code for the dll so I want to modify one if its functions so that it accepts my callback function parameter.
I am doing this in the dll function signature:
void * someDllFunction( int var1, int var2, DataReceivedCallback& callback)
{
callback(2, 200);
}
But I get the error: error C2872: 'DataReceivedCallback' : ambiguous symbol
How can I solve this? Does this have to do with only being allowed to use c-style parameters across dll boundaries??
typedef void (*DataReceivedCallback)(int, int);
should be,
typedef void (MyClass::*DataReceivedCallback)(int, int);
Because, MyCallBackFunction
is a non-static member method of MyClass
. So it cannot have regular function signature.
Also change assignment to,
callback = &MyClass::MyCallBackFunction;
Demo.
You got your types wrong. DataReceivedCallback
, alias void(*)(int, int)
, is a function pointer, but &MyClass::MyCallBackFunction
is a pointer-to-member-function (PTMF). Those two are entirely unrelated and incompatible types!
You cannot treat a member function as a free function.
Since you only have one single instance of your class (Note: "one instance", not "one class"; you always only have one class), why bother with member functions at all? Just make the function global (inside a namespace) and you're done. Though perhaps I'm misunderstanding your requirements.
Another note: You don't need to pass function pointers by reference to the DLL function, just pass them by value. They're just pointers, so they're light-weight.
The thing you are missing is how to declare a pointer to member function and how to invoke the member function via that pointer, below is a working example based on your example:
class MyClass;
//standard way to declare a pointer to member function should be - className::*memberFunName(para list)
typedef void (MyClass::*DataReceivedCallback)(int, int);
class MyClass
{
public:
void AddData(int sourceId, DataReceivedCallback &callback);
void MyCallBackFunction(int var1, int var2);
};
void MyClass::AddData(int sourceId, DataReceivedCallback &callback)
{
callback = &MyClass::MyCallBackFunction;
}
void MyClass::MyCallBackFunction(int var1, int var2 )
{
//do something blah blah
int tem = var1 + var2; //tem = 3 here
}
int main()
{
MyClass obj;
DataReceivedCallback callback;
obj.AddData(1, callback);
(obj.*callback)(1,2); //standard way to call the member function via function pointer
}
精彩评论