0

Hi,
I do have doubt regading use of delete p . In most of the scenarios we call delete inside the destructor if we would have called something int *p = new int (10); but as per below code snippet (original code of delete p)
it already invoke the destructor then call the operator delete then why we should call delete p inside the
destructor.

// Original code: delete a;
if (a != NULL) {
  a->~A();
  operator delete(a);
}

code snippet where we call the delete a inside destructor

class B
{
int *a;
public:
B()
{
a=new int(90);

}
~B()
{
cout<<"B's destructor";
delete a;

}
void dial()
{

cout<<"dial the tone";
}
}; 

~Mohan

2
Contributors
1
Reply
21
Views
4 Years
Discussion Span
Last Post by Elixir42
0

Sorry if I might be wrong but what I have done is to create a ::destroy() function then I can choose to destroy and handle the deallocation of variables when I choose to do so. Preventing double deletion when the program exits and calls the destructor.

Edited by Elixir42

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.