Solved

private destructor

Posted on 1998-11-26
13
555 Views
Last Modified: 2008-03-17
what is the significance of private destructor?  In which cases we can use them and in which we cannt.
0
Comment
Question by:ashu22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
13 Comments
 

Expert Comment

by:KZM
ID: 1178724
A private destructor is useful when you wish to create a class whose instances must only be created on the heap using the new operator.

It is not possible to create an instance of an object on the stack when it has a private destructor.

Note that you have to create some other mechanism to access the destructor (in the example below there is a public member function called Release() which will delete the instance).

Example

class OnlyOnHeap {
public:
    OnlyOnHeap();
    void Release() { delete this; }

    // other public stuff here

private:
    ~OnlyOnHeap();
};

0
 

Author Comment

by:ashu22
ID: 1178725
this is  all understood, but there may be more important cases where the importance of  private destuctor is great ,
0
 
LVL 2

Expert Comment

by:trillo
ID: 1178726
The main purpose of a destructor is to free memory allocated on the Contructor of a class... I don't know what do you mean by "private destructors" since the destructors are almost always defind as public. You should use them if you allocated memory in the constructor using, for example, the "new" keyword. For example:

class X
{
public:
  X ()               //constructor
  {
    c = new char[20];
  }
  ~X ()               //Destructor
  {
    delete []c;
  }
private:
  char *c;
}

If you allocate memory in the constructor but don't define a Constructor, the allocated memory won't be available to other applications when your object is destroyed.... in some cases this causes to terminate the memory resources and you have to restart your PC.
Constructors are called implicitly when you create an object and destructors are called when the object gets out of scope.
When pointers to objects go out of scope, a destructor is not implicitly called. This means that the delete operator must be called to destroy such an object.

Trillo
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ashu22
ID: 1178727
hi man , by private destructor my mean to see the access specifier  for destructor is private,  everyone knows that normally it is in the public side , but there are some speacial case in which it is define as private ,

0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1178728
I guess you want to create instances of that class and destroy them only in some friend class. Then it really doesn't matter if the constructor or destructor is private or not.
Thanks,
pagladsu
0
 

Author Comment

by:ashu22
ID: 1178729
you are really pagla
0
 

Accepted Solution

by:
vmehro earned 50 total points
ID: 1178730
If you are looking for an instance where private destructors find great use,
then COM is the answer. Implementations of AddRef() and Release() assume
that object will only be allocated on heap, as KZM rightly said.
-vmehro
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1178731
to ashu22- so I'm really pagla according to you. Now, tell me, did I really say anything wrong in my previous comment.
0
 

Author Comment

by:ashu22
ID: 1178732
it is right , but i want more elaborate use
0
 

Author Comment

by:ashu22
ID: 1178733
it is right , but i want more elaborate use
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178734
There are actually many reasons why one would want a private or protected destructor.  Basically it is used any time you need to control the circumstances when a class is destroyed.  The examples given are just a few possibilities, but there are many more.
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1178735
Neitod, for curiosity, could you please cite some practical situations when private destructors are used.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178736
There are so many, and most are not text book examples that would be useful to more than a handful of people, but one that I can think of that might be widely used would be objects maintained by smart pointers.  You want to restrict access to the object's destructor so that only smart pointer (which would be a friend) can destroy the object.  

This might also be used for objects that are allocated from private memory pools using the "placement new" operator.  This insures that these objects can't be allocated in other places in memory, like the stack.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 73
Would like to move button in a function 3 93
C++ finding a sting in a char* string from a text file 3 139
White board coding practice 3 99
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question