This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/15505] pure virtual destructor not overloaded, but accepted


------- Additional Comments From giovannibajo at libero dot it  2004-05-20 11:36 -------
(In reply to comment #4)

> Anyway I tried your suggestion of also declaring a destructor for the base
> class and the compiler throws that back in my face with an overloading
> error. So that clearly isn't allowed. 

This compiles correctly with GCC 3.4.0:

----------------------------------------------
extern "C" int printf(const char*, ...);

struct A {
  virtual ~A() = 0;
};

A::~A()
{ printf("A::~A\n"); }

struct B : A {
  ~B() { printf("B::~B\n"); }
};

int main()
{
  B b;
}
----------------------------------------------

and its output is:

------
B::~B
A::~A
------

as expected. If you comment the line with B's destructor definition, it still 
compiles correctly: in fact, B's destructor is synthetized automatically by the 
compiler (as it always happens with all classes), so the type is not abstract 
anymore. 

I am not sure what you tried to do, but it was a wrong try.

> Personally I think that virtual
> destructors are are best dangerous and at worst useless, I just ran across
> it in code I am maintaining.

you are speaking of *pure* virtual destructors here I hope, as virtual 
destructors are very common and very useful.

> As for the correctness of a generated function satisfing pure virtual
> requirements, that totally circumvents the concept of pure virtual. This
> point though goes to my previous statement on the usefulness of such a thing
> so I'd just as soon leave this as "whatever."

It probably circumvents *your* concept of pure virtual. Let me bet something 
that you will be surprised that the following code is actually valid C++, and 
can be compiled, linked and executed correctly by conforming compilers 
(including GCC 3.4.0):


---------------------------------------------
extern "C" int printf(const char*, ...);

struct A {
  virtual void foo(void) = 0;
};

void A::foo(void)
{ printf("A::foo\n"); }

struct B : A {
  virtual void foo(void)
  { printf("B::foo\n"); }
};

int main()
{
  B b;
  b.foo();
  b.A::foo();
}
---------------------------------------------

Its output is what you'd expect, that is:

-------
B::foo
A::foo
-------

A pure virtual function is just a normal virtual function, which just has the 
property of making the class in which it is defined abstract. In my example, A 
is still an abstract type by all means: if you try to declare a variable of 
type A you will get an error. The fact that the pure virtual function has a 
definition does not change a thing. 

The definition of a pure virtual function is required if and only if the 
virtual function is called through the qualified-id syntax (C++ standard, 
[class.abstract]/2). Destructors can be defined pure virtuals, but, as you 
know, they are always called even if you declare an object of a derived type: 
in this case, you always need to provide a definition of the destructor (if you 
define at least an object of that type or any derived type) (C++ standard, 
[class.dtor]/7).

> Not forcing the overloading of that destructor, or even allowing a class to

(BTW: the term "overloading" is inappropriate here. There is no overloading 
going on. I guess you wanted "override")

> quirkily avoid destruction is so dangerous that I can't express it in words,
> but this is what happens with this (the code I am maintaining with this
> error compiled and ran on several systems including one with an older gnu
> compiler) being an accepted syntax. 

No, you are confused. Defining a destructors as pure virtual does not mean that 
there will be no destructors executed for that class. You still have to provide 
a definition for the destructor, in fact.

Hope this clarifies the issue. If you are still not convinced, I suggest you to 
consult a good C++ book about this.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |giovannibajo at libero dot
                   |                            |it


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15505


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]