This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Calling a pure virtual function
- From: Jonathan Wakely <cow at compsoc dot man dot ac dot uk>
- To: Adam Nielsen <a dot nielsen at shikadi dot net>
- Cc: gcc at gcc dot gnu dot org
- Date: Mon, 11 Jul 2005 12:13:38 +0100
- Subject: Re: Calling a pure virtual function
- References: <20050709204147.0aaf5097.a.nielsen@shikadi.net>
On Sat, Jul 09, 2005 at 08:41:47PM +1000, Adam Nielsen wrote:
> Hi all,
>
> I was expecting the following code snippet to work, so am I doing
> something wrong, or is there an issue with GCC? I was under the
> impression that this is allowed, according to
> http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.1
As you've discovered, FAQ 23.3 gives more detail.
> It seems like GCC initially allows it as it starts to compile okay, but
> then I get an undefined reference error from the linker (because it
> seems to be actually calling Base::number(), which obviously won't work
> as it's a pure virtual function.)
It's allowed, and will work, but you have to provide a definition for
the function. Calling number() from the base constructor calls
Base::number() so you have to define it.
GOTW 31 discusses topics related to this: http://www.gotw.ca/gotw/031.htm
GCC is doing exactly the right thing here, it's not possible to tell
that the definition of Base::number() is missing until link time.
jon