Reputation: 229
here is my problem:
I have a function pointer vector and an iterator for it:
std::vector<void* (*) (void*)> f_callbackVector;
std::vector<void* (*) (void*)>::iterator f_callbackVectorIterator = f_callbackVector.begin();
I populate the vector with some functions:
f_callbackVector.push_back((void* (*) (void*))handleDeliveryModeChange);
When I am in the function, I want to be able to check that the iterator holds a pointer to this current function. I tried this:
void handleDeliveryModeChange(SESSION s, const DELIVERY_MODE d1, const DELIVERY_MODE d2) {
if (static_cast<void (*) (void*, DELIVERY_MODE, DELIVERY_MODE)>((*f_callbackVectorIterator)) == &handleDeliveryModeChange) {
LOG_MESSAGE("\n\n\n\nWOW\n\n\n\n");
}
}
and got:
invalid static_cast from type âvoid* (*)(void*)â to type âvoid (*)(void*, DELIVERY_MODE, DELIVERY_MODE)â
I would appreciate some help with this.
Thanks!
UPD: I tried:
if (*(f_callbackVectorIterator) == &handleDeliveryModeChange)
and got error: comparison between distinct pointer types âvoid ()(void)â and âvoid ()(void, DELIVERY_MODE, DELIVERY_MODE)â lacks a cast
I even tried this monster:
if ((*f_callbackVectorIterator) == static_cast<&(void* (*) (void*))>(&handleDeliveryModeChange))
Still no luck, got lots of obscure syntaxis errors.
Upvotes: 0
Views: 4220
Reputation: 14685
You are lying to the compiler:
f_callbackVector.push_back((void* (*) (void*))handleDeliveryModeChange);
handleDeliveryModeChange
does not match this signature. Its signature is actually:
void (*)(SESSION, DELIVERY_MODE, DELIVERY_MODE)
You could make it work by changing to this: Code
#include <vector>
#include <iostream>
typedef void* SESSION; // Or whatever it is
typedef void* DELIVERY_MODE; // You didn't say what these are
typedef void* (*Func) (SESSION, DELIVERY_MODE, DELIVERY_MODE);
typedef std::vector<Func> FuncVector;
FuncVector f_callbackVector;
FuncVector::iterator f_callbackVectorIterator = f_callbackVector.begin();
// Note: Your pointers return void*, so I changed the return type from void:
void* handleDeliveryModeChange
(
SESSION s,
const DELIVERY_MODE d1,
const DELIVERY_MODE d2
)
{
if ((*f_callbackVectorIterator) == &handleDeliveryModeChange)
{
std::cout << "\n\n\n\nWOW\n\n\n\n";
}
return NULL;
}
int main()
{
f_callbackVector.push_back(handleDeliveryModeChange);
}
Upvotes: 1