Pure virtual called -- why uncaught by compiler?

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.
 
T

Tom Widmer

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.

It doesn't even have to "complain" even if you call badIdea directly
from the constructor. Some compilers are kind enough to warn about
this though, but I imagine that some don't.

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.

They warn in the simplest case, but there is no limit to how complex
the code that leads to a pure virtual being illegally called might be,
and it might even depend on runtime factors. e.g.

struct Base
{
Base() { init(false); }
void init(bool b) { if (b) badIdea(); }
virtual void badIdea() = 0;
};

struct Derived : public Base
{
virtual void badIdea() {}
};

int main()
{
Derived d;
return 0;
}

Do you want it to warn for the above code? The code is valid, so a
warning would be annoying at best. Do you start to see what you are
asking of the compiler? For the same reason, the compiler isn't
required to diagnose missing return statements, since it can't in
general determine whether a return statement is required at the end of
a function or not.

Tom
 
D

Derek

Tom said:
Do you want it to warn for the above code? The code
is valid, so a warning would be annoying at best. Do
you start to see what you are asking of the compiler?

Thanks, Tom. I see now why it would be asking too much
of the compiler to flag pure virtual method calls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top