Link to home
Start Free TrialLog in
Avatar of ashu22
ashu22

asked on

private destructor

what is the significance of private destructor?  In which cases we can use them and in which we cannt.
Avatar of KZM
KZM

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();
};

Avatar of ashu22

ASKER

this is  all understood, but there may be more important cases where the importance of  private destuctor is great ,
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
Avatar of ashu22

ASKER

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 ,

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
Avatar of ashu22

ASKER

you are really pagla
ASKER CERTIFIED SOLUTION
Avatar of vmehro
vmehro

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to ashu22- so I'm really pagla according to you. Now, tell me, did I really say anything wrong in my previous comment.
Avatar of ashu22

ASKER

it is right , but i want more elaborate use
Avatar of ashu22

ASKER

it is right , but i want more elaborate use
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.
Neitod, for curiosity, could you please cite some practical situations when private destructors are used.
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.