| typedef size_t (*FUNC)(const char*); void printSize(const char* str) { FUNC f = strlen; (void) printf("%s is %ld chars\n", str, f(str)); } void crashAndBurn(const char* str) { FUNC f = reinterpret_cast<FUNC>(strcat); f(str); } |
| struct Object { }; struct Str : public Object { size_t Len(const char* str) { return strlen(str); } char* Cat(char* s1, const char* s2) { return strcat(s1, s2); } }; typedef size_t (Object::*FUNC)(const char*); void printSize(const char* s) { Str str; FUNC f = static_cast<FUNC>(&Str::Len); (void) printf("%s is %ld chars\n", s, (str.*f)(s)); } void crashAndBurn(const char* s) { Str str; FUNC f = static_cast<FUNC>(&Str::Cat); (str.*f)(s); } |
| Str* pStr = new Str(); FUNC f = static_cast<FUNC>(&Str::Len); (void) printf("%s is %ld chars\n", s, (str->*f)(s)); delete pStr; |