ambiguity in operator delete

Alexander Zvyagin zvyagin@gams.ihep.su
Sat Oct 23 09:16:00 GMT 1999


Dear Martin v. Loewis,

Thank you very much for your mail! I misunderstood a bit how the virtual
base classes work. I totaly missed that my final class C2 has TWO
copies of class A. This was not what I wanted.

But I still have the same qustion. Please, could you help me a little
more?

This is slightly changed code - I removed keyword "virtual". The
error message remains the same.
File b2.c:
class A           { public: void operator delete (void *ptr); };
class N:  public A             {                };
class C1: public A             { public: ~C1(); };
class C2: public C1, public N  { public: ~C2(); };
C2::~C2(){}

$ g++ -c b.c
b2.c: In method `C2::~C2()':
b2.c:5: request for member `operator delete' is ambiguous
b2.c:1: candidates are: static void A::operator delete(void *)
b2.c:1:                 static void A::operator delete(void *)

Class C2 has two copies of class A. If I remove
declaration of A::operator delete the code will be correct - gcc-2.95.1
finds no errors. There is default (global) operator delete for class A and
it does all work. You wrote:

>Now, please note that your class C has two sub-objects of type A: one
>appearing as direct base of N, the other as virtual base of C1. Lookup
>finds an operator delete for each subobject, so the lookup is
>ambiguous.

>Of course, these operators are both defined in the same class, but
>they belong to different subobjects. In the error message, gcc does
>not print the subobject for which a method was found; an experienced
>C++ programmer should be able to see the problem regardless.

This your statement will remain true for default operator delete also!
Note, that all classes (N,C1,C2) are derived from class A, and it does not
matter how these classes inherit class A: as virtual base class or not.
If I have top class A with operator delete and all other classes are
derived from class A, and then I manipulate with deleting objects of
such classes (with class A on the top!) the default operator delete is
simply replaced on A::operator delete. But the code does not compile in
that case!
THAT point confused me. I simply add delete operator for class A and code
(that was good) becomes ill! Was it ill from the beginner? May be I do not
understand how operators new/delete work?

One more argument. I have TWO different objects in class C2. And class A
has operator delete.

               C2
               /\
              /  \
              N  C1
              |  |
              A  A

Each subobject (N,C1) can be deleted without a problem. But GCC can not
delete their both in class C2! There are perfect rules about the order of
creating/deleting objects in the declarations of classes A,N,C1,C2. It
seems it is not important that object of class A will be in two copies
inside class C2 (may be this is not true?).

I still belive that the error report is GCC bug, the code does not have
any ambiguity.

Thank you for your attention,
with best wishes,
Alexander Zvyagin.



More information about the Gcc-bugs mailing list