Topic: Private operator delete in base class


Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Thu, 3 Aug 2006 15:54:03 GMT
Raw View
 Is the following program well-formed?
-----------------------------------8<-----------------------------------
#include <stddef.h>  // size_t

struct B {
       virtual ~B() {}

 private:
       void* operator new(size_t size);
       void operator delete(void* ptr, size_t size);
};

struct D : B { };

int main()
{
 D d;
}
-----------------------------------8<-----------------------------------
 The point is that B::operator delete() is (intentionally) never called but
compilers issue the following error message:

main.cpp: In destructor `virtual D::~D()':
main.cpp:8: `static void B::operator delete(void*, unsigned int)' is private
main.cpp:15: within this context

--
         With all respect, Sergey.               http://ders.angen.net/
         mailto : ders at skeptik.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "keyvan" <ArayeshiAmir@yahoo.com>
Date: Sat, 5 Aug 2006 01:44:03 CST
Raw View
hi

You can not Create Object from D or B becouse you defiine B cnstructor
private & GC can not remove Object d in end of main Becouse B
destructor is private.

you can use this code.
int main()
 {
  D *d;
 }

Amir Arayeshi
ArayeshiAmir@yahoo.com

"Sergey P. Derevyago" wrote:
> Is the following program well-formed?
> -----------------------------------8<-----------------------------------
> #include <stddef.h>  // size_t
>
> struct B {
>        virtual ~B() {}
>
>  private:
>        void* operator new(size_t size);
>        void operator delete(void* ptr, size_t size);
> };
>
> struct D : B { };
>
> int main()
> {
>  D d;
> }
> -----------------------------------8<-----------------------------------
>  The point is that B::operator delete() is (intentionally) never called but
> compilers issue the following error message:
>
> main.cpp: In destructor `virtual D::~D()':
> main.cpp:8: `static void B::operator delete(void*, unsigned int)' is private
> main.cpp:15: within this context
>
> --
>          With all respect, Sergey.               http://ders.angen.net/
>          mailto : ders at skeptik.net
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Greg Herlihy" <greghe@pacbell.net>
Date: Mon, 7 Aug 2006 12:18:44 CST
Raw View
"Sergey P. Derevyago" wrote:
> Is the following program well-formed?
> -----------------------------------8<-----------------------------------
> #include <stddef.h>  // size_t
>
> struct B {
>        virtual ~B() {}
>
>  private:
>        void* operator new(size_t size);
>        void operator delete(void* ptr, size_t size);
> };
>
> struct D : B { };
>
> int main()
> {
>  D d;
> }
> -----------------------------------8<-----------------------------------
>  The point is that B::operator delete() is (intentionally) never called but
> compilers issue the following error message:
>
> main.cpp: In destructor `virtual D::~D()':
> main.cpp:8: `static void B::operator delete(void*, unsigned int)' is private
> main.cpp:15: within this context

This program is ill-formed according to    12.4/11:

"At the point of definition of a virtual destructor (including an
implicit definition (12.8)), the non-array deallocation function is
looked up in the scope of the destructor's class (10.2), and, if no
declaration is found, the function is looked up in the global scope. If
the result of this lookup is ambiguous or inaccessible, or if the
lookup selects a placement deallocation function, the program is
ill-formed."

The explanation is simply: "[ Note: this assures that a deallocation
function corresponding to the dynamic type of an object is available
for the delete-expression (12.5). - end note ]" In other words,
because the compiler cannot guarantee that an object whose dynamic type
is D will never be deleted, it must require that D has an accessible
deallocation function from its destructor.

Greg


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: nikb@webmaster.com ("Nikolaos D. Bougalis")
Date: Tue, 8 Aug 2006 02:54:05 GMT
Raw View
keyvan wrote:

> You can not Create Object from D or B becouse you defiine B cnstructor
> private & GC can not remove Object d in end of main Becouse B
> destructor is private.

 What are you talking about? We're dealing with struct not class, which has a
default access specifier of 'public' so the declared destructor is public.
Also no constructor is provided by the OP, so the compiler will generate a
default public one in his case.

 -n

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "coliveira@gmail.com" <coliveira@gmail.com>
Date: Tue, 8 Aug 2006 07:04:34 CST
Raw View
B::delete is never called directly, but as D derives from B, its delete
operator exists and is private too.

-Carlos

"Sergey P. Derevyago" wrote:
> Is the following program well-formed?
> -----------------------------------8<-----------------------------------
> #include <stddef.h>  // size_t
>
> struct B {
>        virtual ~B() {}
>
>  private:
>        void* operator new(size_t size);
>        void operator delete(void* ptr, size_t size);
> };
>
> struct D : B { };
>
> int main()
> {
>  D d;
> }
> -----------------------------------8<-----------------------------------
>  The point is that B::operator delete() is (intentionally) never called but
> compilers issue the following error message:
>
> main.cpp: In destructor `virtual D::~D()':
> main.cpp:8: `static void B::operator delete(void*, unsigned int)' is private
> main.cpp:15: within this context
>
> --
>          With all respect, Sergey.               http://ders.angen.net/
>          mailto : ders at skeptik.net
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Alberto Ganesh Barbati" <AlbertoBarbati@gmail.com>
Date: Tue, 8 Aug 2006 07:05:17 CST
Raw View
"Sergey P. Derevyago" ha scritto:

> Is the following program well-formed?

According to me, yes, it is. Comeau online and VC7.1 both agree with me
and compile it withouth errors.

> main.cpp: In destructor `virtual D::~D()':
> main.cpp:8: `static void B::operator delete(void*, unsigned int)' is private
> main.cpp:15: within this context

You fail to mention which compiler you are using. It looks like a
compiler bug to me.

Ganesh

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Alberto Ganesh Barbati" <AlbertoBarbati@gmail.com>
Date: Tue, 8 Aug 2006 07:05:58 CST
Raw View
keyvan ha scritto:
> You can not Create Object from D or B becouse you defiine B cnstructor
> private & GC can not remove Object d in end of main Becouse B
> destructor is private.

Hmm... you seems to be totally off-track to me. First, B is not being
defined a private constructor, in fact it is not being defined any
constructor at all, that means it is getting an implicit default
constructor which ought to be public (in case you want to object: no,
the presence of a private operator new does *not* make the constructor
private). Second, B is not being defined a private destructor: as it's
a struct and not a class, the destructor is definitely defined as
public (in case you want to object: no, the presence of a private
operator delete does *not* make the destructor private). Third, what
has GC to do with C++??? Aren't you making confusion with C++/CLI or
(gulp!) C#?

Ganesh

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: non-existent@iobox.com ("Sergey P. Derevyago")
Date: Tue, 8 Aug 2006 16:33:26 GMT
Raw View
Alberto Ganesh Barbati wrote:
> You fail to mention which compiler you are using. It looks like a
> compiler bug to me.

1. g++.exe (GCC) 3.2.3 (mingw special 20030504-1)
a.cpp: In destructor `virtual D::~D()':
a.cpp:8: `static void B::operator delete(void*, unsigned int)' is private
a.cpp:15: within this context

2. Borland C++ 5.6 for Win32 Copyright (c) 1993, 2002 Borland
Error: Unresolved external 'B::operator delete(void *, unsigned int)'
referenced from D:\SRC\TMP2\A.OBJ

3. Digital Mars Compiler Version 8.42n
a.cpp(11) : Error: member 'B::operator del' of class 'D' is not accessible
--
         With all respect, Sergey.               http://ders.angen.net/
         mailto : ders at skeptik.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: nikb@webmaster.com ("Nikolaos D. Bougalis")
Date: Wed, 9 Aug 2006 04:51:55 GMT
Raw View
Alberto Ganesh Barbati wrote:

> Second, B is not being defined a private destructor: as it's
> a struct and not a class, the destructor is definitely defined as
> public (in case you want to object: no, the presence of a private
> operator delete does *not* make the destructor private).

 While that is true, the program is still ill-formed and the compiler is
correct to complain. For details, see the post by Greg Herlihy in this thread.

 -n

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]