Can someone please explain to me why I would be getting
__PureVirtualCalled exception? Can't I call a pure virtual function
from the base class. BTW, as you have seen I did not provide any
implementation for the pure virtual in base class.

Cannot tell. Where do you call 'foo'? From the c-tor?
> Can't I call a pure virtual function
> from the base class.

You can. The result depends on where the call originates.
If it originates from a constructor or the destructor, the
result is undefined.
> BTW, as you have seen I did not provide any
> implementation for the pure virtual in base class.

You can call a pure virtual function from the constructor, but
in that case the call will _not_ be polymorphic. The derived
class object, which overrides those functions, has not been
constructed yet, so the call is resolved statically. If you do
not have a definition (body) for the pure virtual function you
are trying to call from a c-tor, the behaviour is undefined.

It's basically like dereferencing a null pointer.

This is one of the most dramatic differences between C++ and
Java, for example. The latter allows calling virtual functions
from constructor and does that polymorphically. So, you end up
inside a member of the derived class when the derived object
hasn't even begun constructing! Yecch...

Victor, and David thanks for your time. No, I am not calling a virtual
function from the constructor. The problem was in my makefile, I
changed signatures of some of the functions, and that did not force
the recompilation of certain source files. I am guessing that caused
the problem, a complete re-build fixed it.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!