I'm guessing that it needs at least 3 columns
- 1) object type (this must be the index)
2) function name
3) address of function
I don't see why the object type and function name would need to be there.Neo wrote:What exactly are the contents of the vtable in a C++ class with virtual functions?
I'm guessing that it needs at least 3 columnsAlso how can I view the contents of the vtable? Anyone have some code that can do this?
- 1) object type (this must be the index)
2) function name
3) address of function
Code: Select all
class myClass1{public: unsigned int m; myClass1(); ~myClass1();};
class myClass2{public: unsigned int m; myClass2(); ~myClass2();
void virtual vFunction(void){}
};
class myClass3{public: unsigned int m2; myClass2(); ~myClass2();
void vFunction(void){}
};
int main(int argc, char *argv[]){
myClass1 a;
myClass3 c;
unsigned int *ptr = (unsigned int*)&c;
printf("vtable:%x\n", ptr[0]);
printf("myClass1Size:%x\n", sizeof(myClass1));
printf("myClass2Size:%x\n", sizeof(myClass1));
printf("myClass3Size:%x\n", sizeof(myClass1));
printf("
That also leads to the behaviour you'll probably see when you call a virtual function in a base class constructor - it isn't there yet.Kevin McGuire wrote:A interesting little bit was that the constructors for a class are called backwards beginning with the base class, and it is the constructor which writes the address for the vtable in the hidden member field. Each return from a inherited constructor to a caller results in this vtable address being replaced over and over again.
What would RTTI be other than a number indicating its true type? What about putting it in the vtable itself? If so, then you can just use the vtable pointer for the same purpose; I expect that this is done.I know nothing about the RTTI, but I assume by getting clues from the document Solar posted that it could be located at the end of the vtable.