Quote:Actually, I'd like to implement the alarm system myself. How exactly does adding a void* parameter help? Can I pass in a pointer to an object or something?
It doesn't have to be a void*, but as it happens a void* works.
A function pointer is compile-time information only. If you want to be able to have run-time information, you need something analagous to a void*. This can be in the form of a virtual class pointer if required.
With a standard callback (typedef void callback(void*)) and a void*, you can wrap up the unsafe code and do anything required. As an example:
struct TimeData; // structure that stores "when I should be called back" parameters.void InstallCallBack(TimeData, callback, void*); // the void* only callback function.// what I'm actually implementing:template<typename T, typename M>void install_class_callback(TimeData, T* any_class, M* any_trivial_method);template<typename T, typename M>struct class_callback_helper { static void callback_wrapper(void* d) { class_callback_helper* data = (class_callback_helper*)d; data->do_work(); } M* mem_func; T* class_ptr; class_callback_helper(T* class_ptr_, M* mem_func_): class_ptr(class_ptr_), mem_func(mem_func_) {}; void do_work() { class_ptr->mem_func(); delete this; }};template<typename T, typename M>void install_class_callback(TimeData, T* any_class, M* any_trivial_method);{ class_callback_helper<T, M>* helper = new class_callback_helper<T, M>(any_class, any_trivial_method); InstallCallBack(TimeData, class_callback_helper<T, M>::callback_helper, (void*)helper);}
All of the type-unsafe code is distilled into the struct class_callback_helper.
(the above code assumes a given callback is called only once. Repeating callbacks need more work (and the callback code needs to be able to figure out that they will never be called again, so the "void*" can be cleaned up)).
HTH.