I have a situation here – basically:
#include <iostream>
class A {
public:
virtual void foo()=0;
};
class B : A {
public:
void foo() { cout << "I hate this code." << endl; }
void DoSomething() { /* Code */ }
};
A and B are in different files in my case, and much more complex needless to say – but here is my problem:
Somewhere within the class B in a function (like DoSomething()), I call foo. Now, foo is a pure-virtual function from A properly defined in B, so it works fine – the compilation is fine too.
If I call it like:
B::foo()
it works great.
If I call it like:
foo()
It hangs the system at run time. Why would scoping operators change the outcome when the function is not static or anything like that anyway?
PS: I wrote that code on the spot and didn’t have a compiler for this question, so sorry for typos if there are any.
The code looks correct. This strongly implies that the vptr or vtable have been corrupted by some out-of-bounds memory access, or that the
thispointer is invalid.The scoping operator changes the call by allowing it to bypass the virtual function lookup altogether.