Skip to content

Instantly share code, notes, and snippets.

@cleak
Created January 21, 2019 05:49
Show Gist options
  • Save cleak/122306c8dbab913e811ff892d27472f0 to your computer and use it in GitHub Desktop.
Save cleak/122306c8dbab913e811ff892d27472f0 to your computer and use it in GitHub Desktop.
template <typename T>
void** GetVTable(T* obj) {
return *((void***)obj);
}
@md2perpe
Copy link

Why cast to void***? Isn't void* enough?

@cleak
Copy link
Author

cleak commented Sep 22, 2019

Why cast to void***? Isn't void* enough?

The final desired type is void**(because it's a pointer to a table of pointers). The given obj pointer though is a pointer to an object that contains a pointer to that table, which adds the third layer of indirection that needs to be de-referenced.

You could get away with returning void* in which case you'd have return *((void**)obj);, but that makes the final table a bit more difficult to work with, since that will need to be re-cast.

@md2perpe
Copy link

Will you get a type error (or warning) if you just use

template <typename T>
void** GetVTable(T* obj) {
  return *((void*)obj);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment