Se original question in the bottom.
I think I understand what you guys are saying now – that because the internal structure of the member function pointer is compiler/machine specific it is really not possible to do that I am trying to. So even though it works when I test it – I have no guarantee that it will on other compilers/machines.
Is there another way to go about what I want then?
I have a template class and a base template class for that class, and I have a delegate class which contains a std::map of all the events the delegate class should invoke, when invoked.
The reason i need a map is, both to insure that the same member function (event pointing to member function) is not adde more that once, and to make it possible to remove events from the map using the object and member function originally used when instantiating the event object.
template <class T_arg1> struct EventBase1
{
public:
bool operator < (const EventBase1 &event1) const { return _key < event1._key; };
virtual void operator()(T_arg1 t1) const {};
std::pair<intptr_t, intptr_t> _key;
};
template <class T, class T_arg1> struct Event1: public EventBase1<T_arg1>
{
template <class T_arg1> friend class Delegate1;
typedef typename void (T::*Method)(T_arg1);
private:
Method _method;
T* _object;
public:
Event1(T* object, Method method): _object(object), _method(method)
{
_key = std::pair<intptr_t, intptr_t>(reinterpret_cast<intptr_t>(object), reinterpret_cast<intptr_t>( reinterpret_cast<void*&>(method)));
};
virtual void operator()(T_arg1 t1) const {
(_object->*_method)(t1);
};
};
template <class T_arg1> class Delegate1
{
public:
typedef typename EventBase1<T_arg1> T_event;
void operator += (T_event* observer)
{
assert(observer);
_observers[*observer] = observer;
};
void operator -= (const T_event &observer)
{
std::map<T_event, T_event*>::iterator i = _observers.find(observer);
if(i != _observers.end()) {
delete i->second;
_observers.erase(i);
}
};
void operator()(T_arg1 t1)
{
for(std::map<T_event, T_event*>::iterator i = _observers.begin(); i != _observers.end(); i++) {
(*(i->second))(t1);
}
};
private:
std::map<T_event, T_event*> _observers;
};
Original question:
I am storing function pointers in a std::map
, and I am generating my key for the map as follows: std::pair<int, int>( (int)((int*)object), (int)(static_cast<const void*>(&method)) )
.
method
is a function (method) pointer, and object
is a pointer to the object of the method.
It works, however I have a sneaky suspicion that the way I get the second part of the key isn’t entirely correct.
I have never fully understood function pointers, but I guess that I am getting the address of the pointer and not the address of the function, and the compiler won’t let me do like this ((int)(static_cast<const void*>(method)))
.
So my question is - how do I get a unique key from the function pointer which will the same if I later get a get the key from another function pointer pointing the same method?
Thanks in advance, Martin