• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

C++; Is there a way to check for a virtual function table?

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

mccoyn

Senior Member
Joined
Nov 17, 2003
Location
Michigan, USA
I want a macro or a template function that will determine if a class has a vft. Ultimatly, I want something that will reset all the fields of a class to 0 except the vft.

Something similiar to doing this in C:
memset(&obj, 0, sizeof(obj));

This works on any struct or class as long as it doesn't have virtual functions. When it does have virtual functions, the vft pointer gets reset to NULL and the virtual functions no longer work. I'd like something like this:

Code:
void classset<T>(T* obj){
   if (has_vft<T>()){
     memset((char*)(obj) + sizeof(void*), 0, sizeof(T) - sizeof(void*));
   } else {
     memset(&obj, 0, sizeof(T));
   }
}

Its the has_vft<T> function I don't know how to write.
 
No way since VFTs are not really standardized across C++ compilers and are not accessible from C++ itself. Layout of them is a compiler implemenation specific (and even optimization specific) thing.
 
still, you can't really do it since the compiler won't give you any info about it and depending on your compiler switches (release build or debug build) it might be totally different. And even if you forever use MSVC: MSVC is developed further too, which can (and will, since we all know MS) break backwards compatibility with this feature.
 
I'm well aware of all those things. I still want to do it. Anyone have any ideas of how it might be possible?
 
yes, by learning assembly (seriously): You create a small test program which uses virtual functions/classes and then disassemble it. Study the assembly source and poke it in a debugger looking how the compiler lays out the tables. Then you can write your own (assembly)functions to change the table.
 
Back