Re: A simple problem with an abstract class...
Ben Voigt wrote:
Do any partially constructed objects get handed off to B? The virtual table
is initialized incrementally, so calls to virtual functions made from inside
the base constructor aren't made virtually.
The easiest way to debug such a situation is to implement A::x:
#ifndef NDEBUG
void A::x()
{
DebugBreak();
}
#endif
This will make your debugger stop on the pure virtual function with your
debug build. Then you can inspect the callstack, and see exactly how
this pure virtual function got called.
One time I had this problem with a complex library, and it was exactly
as Ben suggested, I was calling a pure virtual function from a
constructor. The solution was to rethink the entire design, removing all
virtual function calls from constructors.
Here's my recommendation to the OP. If you must call a virtual function
from a constructor, consider hiding the constructor, replacing it with a
static Create function:
class C
{
public:
static C* Create(int param)
{
C* p = new C(param);
p->VirtualFunc();
return p;
}
protected:
C(int param);
virtual ~C() { } // you need this, even if it's empty
private:
virtual void VirtualFunc() = 0;
};
The disadvantage is that this class now can't be created on the stack.
Here's how to use it:
C* p = C::Create(111);
delete p;
Also consider makig Create return boost::shared_ptr<C>, and then you
have really safeguarded yourself against the worst programming errors
(memory leaks, uninitialized pointers, reusing dead pointers, double
deletion, and virtual functions called from constructor).
Tom