473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can a destructor be declared private?

Is it possible to declare a class destructor as private?

Jul 23 '05 #1
8 7463
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


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
True, but if the class/function were made a friend, it would have
access to the class's destructor.

Jul 23 '05 #4
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
7
by: Christopher Benson-Manica | last post by:
I swear, this godforsaken __fastcall qualifier that Borland came up with is going to drive me insane! All I want is to subclass from a class A with a destructor declared virtual __fastcall...
7
by: lovecreatesbeauty | last post by:
The following shortened table comes from a big company's C++ technical document accompanied with its C++ compiler. This company's C++ compiler was/is very popular in Windows platform programming....
5
by: Tony Johansson | last post by:
Hello!! Is it possible to have abstract destructor. //Tony
15
by: __PPS__ | last post by:
Hello everybody, in my exam in c++ I answered that destructors may be declared private, public or protected. The question was: "What possible scopes (public or private) can a destructor have?" ...
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
6
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made...
5
by: Ramkey | last post by:
I was asked in a test class A{ A ~A{ cout << "A's destructor"; } }; int main(void){ A Aobj; Aobj.~A;
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.