By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,118 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,118 IT Pros & Developers. It's quick & easy.

Can a destructor be declared private?

P: n/a
Is it possible to declare a class destructor as private?

Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Mon, 18 Jul 2005 15:08:02 +0000 (UTC), Alfonso Morra
<sw***********@the-ring.com> did courageously avow:
Is it possible to declare a class destructor as private?


Try it and see.

I would venture not though because the program would no longer be able
to call it. :-)

Ken Wilson
"Coding, coding, over the bounding main()"
Jul 23 '05 #2

P: n/a


Ken Wilson wrote:
On Mon, 18 Jul 2005 15:08:02 +0000 (UTC), Alfonso Morra
<sw***********@the-ring.com> did courageously avow:

Is it possible to declare a class destructor as private?

Try it and see.

I already did and found the answer (that's why I deleted the OP).
I would venture not though because the program would no longer be able
to call it. :-) Not necessarily true. This is a nested class that must not be accesible
outside its parent contaner. The container object is declared a friend
and therefore has access to the nested classes private parts.
Ken Wilson
"Coding, coding, over the bounding main()"


Jul 23 '05 #3

P: n/a
True, but if the class/function were made a friend, it would have
access to the class's destructor.

Jul 23 '05 #4

P: n/a
On 18 Jul 2005 08:22:13 -0700, "titancipher" <ja***********@gmail.com>
did courageously avow:
True, but if the class/function were made a friend, it would have
access to the class's destructor.


Fair enough. I was thinking about making the nested class private to
the container and then the destructor could be made public.

Ken Wilson
"Coding, coding, over the bounding main()"
Jul 23 '05 #5

P: n/a
Ken Wilson wrote:
On Mon, 18 Jul 2005 15:08:02 +0000 (UTC), Alfonso Morra
<sw***********@the-ring.com> did courageously avow:
Is it possible to declare a class destructor as private?
Try it and see.


There exists a well-known compiler (from a very big company) that
ignores the private-ness of destructors.
I would venture not though because the program would no longer be able
to call it. :-)


Hint: friendship

Jul 23 '05 #6

P: n/a
"Mercator" <me********@spambob.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com
Ken Wilson wrote:
On Mon, 18 Jul 2005 15:08:02 +0000 (UTC), Alfonso Morra
<sw***********@the-ring.com> did courageously avow:
Is it possible to declare a class destructor as private?


Try it and see.


There exists a well-known compiler (from a very big company) that
ignores the private-ness of destructors.


If it is the company that I am thinking of, then I am unable to reproduce
this. In particular, the following code won't compile on versions 6.0, 7.1
and 8.0 Beta 2 (I haven't tested 7.0):

class Test
{
~Test(){}
};
int main()
{
Test t;
}
--
John Carson

Jul 23 '05 #7

P: n/a
John Carson wrote:
If it is the company that I am thinking of, then I am unable to reproduce
this. In particular, the following code won't compile on versions 6.0, 7.1
and 8.0 Beta 2 (I haven't tested 7.0):


You are right. I confused it with a problem I had some time ago using a
private operator delete.

Jul 23 '05 #8

P: n/a
See Scott Meyers, More effective C++, item 27.
If you want to force the creation of an object on the heap,
then you use a private destructor and provide a public
member function destroy.

class Demo {
~Demo() {} // private
public:
void destroy() const
{
delete this;
}
};

Now the following won't work:

void foo()
{
Demo d;
}

regards, Stephan

Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.