I am getting a segmentation fault whenever I am trying to access a virtual function. The code is basically like this:
class Super {
public:
Super() { cout << "Ctor Super" << endl; }
virtual void test() = 0;
};
class Sub : public Super {
public:
Sub() { cout << "Ctor Sub" << endl; }
void test() { cout << "Test in Sub" << endl; }
};
void main()
{
Super* s = new Sub;
s->test(); // Segmentation fault So I tried the one below
Sub* s1 = new Sub;
s1->test(); //Still segmentation fault
Sub s2;
s2.test(); // Works fine BUT
Super *s3 = &s2;
s3->test(); // segmentation fault and EVEN
Sub *s4 = &s2;
s4->test(); //segmentation fault
}
I have tried almost everything I know about virtual functions and it does not work. It's actually a part of a bigger program so it might have some problem there but as soon as I remove the virtual function or stop making it virtual it works. Any Ideas?
Also is there any tool or way to examine the vtable?