By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,905 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,905 IT Pros & Developers. It's quick & easy.

Pure virtual called -- why uncaught by compiler?

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Tue, 26 Oct 2004 11:45:28 -0400, Derek <us**@nospam.org> wrote:
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 assumingthere 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
Jul 22 '05 #2

P: n/a
Tom Widmer wrote:
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.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.