D
Derek
The following simple program causes a runtime error
(pure virtual method called):
struct Base
{
Base() { init(); }
void init() { badIdea(); }
virtual void badIdea() = 0;
};
struct Derived : public Base
{
virtual void badIdea() {}
};
int main()
{
Derived d;
return 0;
}
I understand why this error happens. The Derived ctor
invokes the Base ctor, which calls init(), which calls
the pure virtual Base::badIdea() (not Derived::badIdea(),
because Derived doesn't exist yet).
My question: why can't the compiler catch this? It
complains if I try to call badIdea() directly from the
Base() ctor, but not in the example above. I'm assuming
there is a good reason why compilers can't diagnois
this problem because neither MSVC 6, Comeau 4.3.3, nor
GCC 3.3.3 see anything wrong with the above program.
Thanks.
(pure virtual method called):
struct Base
{
Base() { init(); }
void init() { badIdea(); }
virtual void badIdea() = 0;
};
struct Derived : public Base
{
virtual void badIdea() {}
};
int main()
{
Derived d;
return 0;
}
I understand why this error happens. The Derived ctor
invokes the Base ctor, which calls init(), which calls
the pure virtual Base::badIdea() (not Derived::badIdea(),
because Derived doesn't exist yet).
My question: why can't the compiler catch this? It
complains if I try to call badIdea() directly from the
Base() ctor, but not in the example above. I'm assuming
there is a good reason why compilers can't diagnois
this problem because neither MSVC 6, Comeau 4.3.3, nor
GCC 3.3.3 see anything wrong with the above program.
Thanks.