I'm using VS 2013 and trying to see how vptr and vftable are working at object level. So I have the following classes:
#include<iostream>
using namespace std;
class baseClass
{
public:
void nonVirtualFunc() {}
virtual void virtualNonOverriddenFunc() {}
virtual void virtualOverriddenFunc() {}
};
class derivedClass : public baseClass
{
public:
virtual void virtualOverriddenFunc() {}
virtual void derivedClassOnlyVirtualFunc() { cout << "derivedClass" << endl; }
};
int main(int argc, char** argv) {
derivedClass derivedClassObj2;
cout << "Size of derivedClassObj: " << sizeof(derivedClassObj2) << endl;
return 0;
}
And this is what I see when debugging:
Theoretically there should be TWO vptrs. One for the vftable of baseClass and one for derivedClass to track the newly added derivedClassOnlyVirtualFunc().
But as you see, there is only one vptr/vftable. But the mechanism works fine.
I thought there is second vptr that I cannot see in the watch window, so I printed out the size of the object. It is 4 bytes, indicating that only one pointer is present.
So how is this working with the newly added virtual function?
According to this there should be two vptrs.
EDIT: I checked the memory contents of vftable as Serge suggested and there indeed are three entries.
For some reason it is not showing up in the debugger.
Cheers.