Who can explain this code for me?about virtual function
-
/////////////////////////////////////////////// // CIOMessageMap.h class __declspec(novtable) CIOMessageMap { public: virtual bool ProcessIOMessage(IOType clientIO, ClientContext* pContext, DWORD dwSize) = 0; }; #define BEGIN_IO_MSG_MAP() \ public: \ bool ProcessIOMessage(IOType clientIO, ClientContext* pContext, DWORD dwSize = 0) \ { \ bool bRet = false; #define IO_MESSAGE_HANDLER(msg, func) \ if (msg == clientIO) \ bRet = func(pContext, dwSize); #define END_IO_MSG_MAP() \ return bRet; \ }
Why class CIOMessageMap should be declared by declspec(novtable)? The macros implements the member function of CIOMessageMap --ProcessIOMessage();But in other places of project,there no any inheritances of CIOMessageMap. Who can tell me the advantages about this coding style? There are will best in detail.;) -
/////////////////////////////////////////////// // CIOMessageMap.h class __declspec(novtable) CIOMessageMap { public: virtual bool ProcessIOMessage(IOType clientIO, ClientContext* pContext, DWORD dwSize) = 0; }; #define BEGIN_IO_MSG_MAP() \ public: \ bool ProcessIOMessage(IOType clientIO, ClientContext* pContext, DWORD dwSize = 0) \ { \ bool bRet = false; #define IO_MESSAGE_HANDLER(msg, func) \ if (msg == clientIO) \ bRet = func(pContext, dwSize); #define END_IO_MSG_MAP() \ return bRet; \ }
Why class CIOMessageMap should be declared by declspec(novtable)? The macros implements the member function of CIOMessageMap --ProcessIOMessage();But in other places of project,there no any inheritances of CIOMessageMap. Who can tell me the advantages about this coding style? There are will best in detail.;)ProcessIOMessage()
is defined asvirtual
, so the compiler wants to add a vtable for the class. But, because it's an abstract class (due to the= 0
) it cannot be instantiated and doesn't need the overhead of the vtable.__declspec(novtable)
instructs the compiler to not generate a vtable. A more detailed explanation can be found here[^].Cyril Connolly wrote:
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.