I've been trying to follow some of this advice:
https://softwareengineering.stackexchange.com/a/213635/46534
and
https://en.wikipedia.org/wiki/Function_object
But struggling to get it to compile.
I have a delegate definition:
struct SomeDelegate {
void operator()(SomeType *data) {
//do some stuff with data
}
};
And then a member function which accepts a function pointer:
void DoSomethingThatCallsback(void(*callback)(SomeType *) ) {
callback(ptrToSomeType);
}
And then when I try and use this member function as follows:
foo.DoSomethingThatCallsback(SomeDelegate());
I get the compile error:
cannot convert argument 1 from 'SomeDelegate' to 'void (__cdecl *)(Core::SomeType *)'
All the examples I've been reading suggest this is possible.
I've tried using templates like this:
template <typename Callback>
void DoSomethingThatCallsback(Callback callback)
But I get a similar error.
I'm ultimately looking for a more OOP approach to resorting to function pointers or moving to C++11. Also, unable to use the STL.
Updated with more context
struct MapOpenedDelegate {
public:
void operator()(Map *openedMap) {
}
};
class MapReader {
public:
template <typename Callback>
void RegisterMapOpenedCallback(Callback &callback) {
_callbacks.Add(callback);
}
private:
Rise::List<void(*)(Map *)> _callbacks;
};
...
mapReader.RegisterMapOpenedCallback(MapOpenedDelegate());