Subj : Re: Can you mimic atexit() for class functions? To : borland.public.cpp.borlandcpp From : "Benjamin Pratt" Date : Wed Oct 08 2003 08:52 pm I assume you only used the atexit() function as an example of callback functionality, not that this "CallbackContainer" would invoke the callbacks during exit. That said, obviously the difficulity in this "generic" callback container is that the class member functions have a this pointer, making prototyping the function a bit difficult. However, not impossible. Here is an example that most closly mimics what you are trying to do: #include #include typedef void (*CALLBACK_PTR_FUNC)(void*); class CallbackContainer { typedef std::pair FUNC_THIS; typedef std::vector CALLBACKS; CALLBACKS callbacks; public: void AddCallback(CALLBACK_PTR_FUNC func, void* pThis); void DoCallbacks(); } g_CallbackContainer; class A { public: A(int value) : _value(value) { g_CallbackContainer.AddCallback((CALLBACK_PTR_FUNC)Callback, this); } static void Callback(void* pThis) { s)->CallbackImpl(); } void CallbackImpl() { std::cout << "A::CallbackImpl() = " << _value << std::endl; } private: int _value; }; class B { public: B(float value) : _value(value) { g_CallbackContainer.AddCallback((CALLBACK_PTR_FUNC)Callback, this); } static void Callback(void* pThis) { static_cast(pThis)->CallbackImpl(); } void CallbackImpl() { std::cout << "B::Callback() = " << _value << std::endl; } private: float _value; }; void CallbackContainer::AddCallback(CALLBACK_PTR_FUNC func, void* pThis) { callbacks.push_back(FUNC_THIS(func, pThis)); } void CallbackContainer::DoCallbacks() { for(CALLBACKS::iterator p = callbacks.begin(); p != callbacks.end(); ++p) (*p->first)(p->second); } int main(void) { A a1(1), a2(2), a3(3); B b1(1.5), b2(2.5), b3(3.5); g_CallbackContainer.DoCallbacks(); return 0; } However, that said, there is IMO, a better way to do this in C++. (To follow in the next post) .