c++ - How to make safer AddCallback() - function for different events in object -


i have addcallback() witn second parameter int. how reduce the
possibility of transmission of incorrect type client code? style situation? note: type incorrect if not equal mousedown mouseup mousehover mouseleave

const int  mousedown = 0; const int  mouseup = 1; const int  mousehover = 2; const int  mouseleave = 3;  class widget { public:     ...     bool addcallback(void (*func)(widget*), const int type); protected:     std::vector<void (*)(widget*)>  funcsdown;     std::vector<void (*)(widget*)>  funcsup;     std::vector<void (*)(widget*)>  funcshover;     std::vector<void (*)(widget*)>  funcsleave;     ... }; 

with c++11, should use strongly-typed enum:

enum class mouseaction { mousedown = 0, mouseup = 1, mousehover = 2, mouseleave = 3 };  bool addcallback(void (*func)(widget*), const mouseaction type); 

with c++03, still use enum , provide second overload make calls using integer turned linker error:

enum mouseaction { mousedown = 0, mouseup = 1, mousehover = 2, mouseleave = 3 };  // implement method (here or somewhere else) bool addcallback(void (*func)(widget*), const mouseaction type);  // declare one: bool addcallback(void (*func)(widget*), const int type); 

if call this:

mywidget.addcallback( &foo, mousedown ); // works mywidget.addcallback( &foo, 42 ); // linker error 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

php - HTTP_REFERER woes: How can I allow access to a specific page, only when a visitor has visited another specific page beforehand? -

java Extracting Zip file -