I'm making a class to listen to a buffer, then when it recieves a word, it broadcasts (emits) that word (signal) to registered functions (slots). My issue is that I'd like to be able to register methods of unknown class types instead of just C functions. This is easy for C:
It's very simplified below to help me concentrate on my question.
class buf_rx
{
public:
typedefvoid(*pRxFunc)(int);
private:
pRxFunc m_process;
public:
void receive()
{
check fornew words
for each new word
m_process(word);
}
void registr ( pRxFunc function )
{
m_process = function;
}
};
To use a pointer to a method, I also need a pointer to the class, but I'm not sure how to save the class type:
class buf_rx
{
private:
void* m_class; // The type is wrong here,
void (*m_method)(int); // I need a class definition before m_method
public:
void receive()
{
check fornew words
for each new word
m_class->m_process(word);
}
template <class T>
void registr( T* pClass, void(T::*pFunc)(int) )
{
m_class = pClass;
m_method = pFunc;
}
};
But this doesn't work. Any ideas? I know Qt's signals/slots would work, but I am trying to keep this code independant of 3rd party libraries.
Thanks Thomas, you're right that's probably the simplest way to go. I was trying to stay away from inheritance because that method seemed very Java-ey to me, but the more I think of it the more I like it. Thanks for the idea: