Working of the delete keyword | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Working of the delete keyword

It says on here thst deleting a pointer pointing to some object of a class will destroy the object but that’ll only happen if the object is dynamically allocated right? Why would a normal object be destroyed if we delete a pointer pointing to it?!? Won’t just the pointer be deleted, and the object still stay there?

9th Oct 2017, 8:02 AM
Alpha Mineron
Alpha Mineron - avatar
3 Answers
+ 12
To see the full article from MSDN (which Apoorva fetch the information from there) see: [https://msdn.microsoft.com/en-us/library/h6227113.aspx]
9th Oct 2017, 8:38 AM
Babak
Babak - avatar
+ 7
When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.
9th Oct 2017, 8:20 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 2
The delete operator takes a pointer to a primitive or an object. Without a pointer, it wouldn't know what to delete. They can be overloaded, so it doesn't necessarily have to be real dynamic memory.
9th Oct 2017, 8:15 AM
aklex
aklex - avatar