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