Home > Back-end >  C in the delete specific stem what? Only the destructor will call step encapsulate it for me?
C in the delete specific stem what? Only the destructor will call step encapsulate it for me?

Time:10-10

delete in c + + specific stem what? Only the destructor will call step encapsulate it for me? If I write a public interface specifically manually calling the destructor, whether can replace the delete?
 
The class MyClass
{
Protected:
Char * STR;
Public:
MyClass () {cout & lt; <"Structure" & lt; ~ MyClass () {cout & lt; <"Destruction" & lt; Void myCle () {this - & gt; ~ MyClass (); }
};

Int main ()
{
MyClass * p=new MyClass;
P - & gt; MyCle ();
//delete p;
return 0;
}

CodePudding user response:

Suggest to look at c + +; A class of pointer is released, the need to manually delete. Is a class member is a pointer in the destructor is released, the two aren't the same

CodePudding user response:

The destructor is used to clean up the cursor dynamically allocated memory space in the class object,
Can't clean up the class object itself takes up space,
The Delete function can,

CodePudding user response:

The delete key to do two things, the destructor call p - & gt; ~ MyClass (), as well as calling free (p);
If need to use the delete new [], [], the delete [] to record the number of according to the new, circular calling the destructor,

CodePudding user response:

Normal use is not recommended during their operation, because memory release problems,
You now want to manage memory, you can refer to the new overloaded the direction of knowledge,

CodePudding user response:

Delete do two things:
First, the free memory
Second, calling the destructor

CodePudding user response:

refer to the second floor zjq9931 response:
in the destructor is used to clean up the class object pointer to dynamically allocated memory space,
Can't clean up the class object itself takes up space,
The Delete function can,

Thanks, I see the

CodePudding user response:

references of the dream life (4th floor response:
normal use is not recommended during their operation, because memory release problems,
You now want to manage memory, you can refer to the new overloaded the direction of knowledge,

Did encounter situation need to manually call the destructor is used, I look at the related knowledge, thank you
  • Related