C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Private operator delete in base class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Sergey P. Derevyago
Guest





PostPosted: Thu Aug 03, 2006 8:54 pm    Post subject: Private operator delete in base class Reply with quote



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 ]
Back to top
keyvan
Guest





PostPosted: Sat Aug 05, 2006 7:44 am    Post subject: Re: Private operator delete in base class Reply with quote



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 (AT) yahoo (DOT) com

"Sergey P. Derevyago" wrote:
Quote:
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 ]
Back to top
Greg Herlihy
Guest





PostPosted: Mon Aug 07, 2006 6:18 pm    Post subject: Re: Private operator delete in base class Reply with quote



"Sergey P. Derevyago" wrote:
Quote:
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.Cool), 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 ]
Back to top
Nikolaos D. Bougalis
Guest





PostPosted: Tue Aug 08, 2006 7:54 am    Post subject: Re: Private operator delete in base class Reply with quote

keyvan wrote:

Quote:
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 ]
Back to top
coliveira@gmail.com
Guest





PostPosted: Tue Aug 08, 2006 1:04 pm    Post subject: Re: Private operator delete in base class Reply with quote

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:
Quote:
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 ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Tue Aug 08, 2006 1:05 pm    Post subject: Re: Private operator delete in base class Reply with quote

"Sergey P. Derevyago" ha scritto:

Quote:
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.

Quote:
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 ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Tue Aug 08, 2006 1:05 pm    Post subject: Re: Private operator delete in base class Reply with quote

keyvan ha scritto:
Quote:
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 ]
Back to top
Sergey P. Derevyago
Guest





PostPosted: Tue Aug 08, 2006 9:33 pm    Post subject: Re: Private operator delete in base class Reply with quote

Alberto Ganesh Barbati wrote:
Quote:
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 ]
Back to top
Nikolaos D. Bougalis
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: Private operator delete in base class Reply with quote

Alberto Ganesh Barbati wrote:

Quote:
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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.