Ideally delete operator should not be used for this pointer. However, if used, then following points must be considered.
1) delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise behavior is undefined.
2) Once delete this is done, any member of the deleted object should not be accessed after deletion.
The best thing is to not do delete this at all.
Thanks to Shekhu for providing above details.
This article is contributed by Rahul Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to create an unordered_map of user defined class in C++?
- Sum of Bitwise-OR of all subarrays of a given Array
- attributes in C++
- Find sum of a number and its maximum prime factor
- How to traverse a C++ set in reverse direction
- How to Read and Print an Integer value in C++
- How to Read and Print an Integer value in C
- Difference between Definition and Declaration
- Nested switch case
- Inline function in C
- Find the Nth term of the series 14, 28, 20, 40,.....
- How to traverse a STL map in reverse direction?
- CSV file management using C++
- std::any Class in C++
- Loader in C/C++
Improved By : DeepakTiwari2