Bug 35772 - GCC allows defining pure virtual functions
Summary: GCC allows defining pure virtual functions
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.1.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-03-30 22:17 UTC by yuri
Modified: 2008-03-31 07:10 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description yuri 2008-03-30 22:17:43 UTC
GCC compiles the code below without any error:

//----------------------------------
class A {
protected:
  virtual void foo() const = 0;
};

// Defining pure virtual functions should not be allowed.
void A::foo() const 
{
}
//----------------------------------
Comment 1 Björn A. Herwig 2008-03-31 06:41:31 UTC
Hi yuri,

I think, this is perfectly correct code and GCC is right in accepting it. First of all, see "Effective C++" issue 14 about the pure virtual destructor. Then see here:

http://en.wikipedia.org/wiki/Virtual_function#Abstract_classes_and_pure_virtual_functions

<cite>
Although pure virtual methods typically have no implementation in the class that declares them, pure virtual methods in C++ are permitted to contain an implementation in their declaring class, providing fallback or default behaviour that a derived class can delegate to if appropriate.
</cite>

Regards,
Björn Herwig

(In reply to comment #0)
> GCC compiles the code below without any error:
> 
> //----------------------------------
> class A {
> protected:
>   virtual void foo() const = 0;
> };
> 
> // Defining pure virtual functions should not be allowed.
> void A::foo() const 
> {
> }
> //----------------------------------
> 

Comment 2 yuri 2008-03-31 07:10:15 UTC
Hi Björn,

Thank you for the link and setting me straight.  You are correct, implementation of a pure virtual function by the class that declares it makes sense. It is just the class itself remains abstract.

Earlier today I had a problem with dynamic library loading and it disappeared only after I removed an implementation of a pure virtual method from a declaring class.  I thought this was a problem, but it looks like I need to look more at what was happening.

Regards,
Yuri

(In reply to comment #1)
> Hi yuri,
> 
> I think, this is perfectly correct code and GCC is right in accepting it. First
> of all, see "Effective C++" issue 14 about the pure virtual destructor. Then
> see here:
> 
> http://en.wikipedia.org/wiki/Virtual_function#Abstract_classes_and_pure_virtual_functions
> 
> <cite>
> Although pure virtual methods typically have no implementation in the class
> that declares them, pure virtual methods in C++ are permitted to contain an
> implementation in their declaring class, providing fallback or default
> behaviour that a derived class can delegate to if appropriate.
> </cite>
> 
> Regards,
> Björn Herwig
> 
> (In reply to comment #0)
> > GCC compiles the code below without any error:
> > 
> > //----------------------------------
> > class A {
> > protected:
> >   virtual void foo() const = 0;
> > };
> > 
> > // Defining pure virtual functions should not be allowed.
> > void A::foo() const 
> > {
> > }
> > //----------------------------------
> > 
>