I have a layout (simplified) as follows :
Expand|Select|Wrap|Line Numbers
- template <class T>
- class MsgHandler
- {
- protected :
- typedef void (T::*handle_t)(Message *);
- handle_t _handles[128];
- public :
- void procMsg()
- {
- Message * msg = EventMgr::GetMessage();
- (reinterpret_cast<T *>(this)->*_handles[msg->evt()])(msg);
- }
- };
- class Amgr : public MsgHandler<Amgr>
- {
- public :
- Amgr():MsgHandler(TGT_ALPHA)
- {
- _handles[ALPHA_SET_VAL] = &Amgr::OnChangeInt;
- }
- void OnChangeInt(Message * msg)
- {
- ...
- }
- };
I'm worrying about procMsg() in MsgHandler, is it safe to cast the 'this' pointer into the derived class, in order to call a function which *might* affect the derived class variables??
It's the only way I've found that the base class can use the derived class functions, without actually knowing about them and without writing the same code for every class that inherits from MsgHandler.
Thanks in advance,
babis