Can you please elaborate why this code crashes at the places mentioned? I am a bit stumped on this. I guess that it has got something to do with sizeof(int)
but I am not so sure. Can anybody explain?
class Base
{
public:
virtual void SomeFunction()
{
printf("test base
");
}
int m_j;
};
class Derived : public Base {
public:
void SomeFunction()
{
printf("test derive
");
}
private:
int m_i;
};
void MyWonderfulCode(Base baseArray[])
{
baseArray[0].SomeFunction(); //Works fine
baseArray[1].SomeFunction(); //Crashes because of invalid vfptr
baseArray[2].SomeFunction(); //Crashes because of invalid vfptr
baseArray[3].SomeFunction(); //Works fine
baseArray[4].SomeFunction(); //Crashes because of invalid vfptr
baseArray[5].SomeFunction(); //Crashes because of invalid vfptr
baseArray[6].SomeFunction(); //Works fine
baseArray[7].SomeFunction(); //Crashes because of invalid vfptr
baseArray[8].SomeFunction(); //Crashes because of invalid vfptr
baseArray[9].SomeFunction(); //Works fine
}
int _tmain(int argc, TCHAR* argv[])
{
Derived derivedArray[10];
MyWonderfulCode(derivedArray);
return 0;
}
See Question&Answers more detail:os