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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C program to count frequency of each element in an array
- Higher Order Functions in C++
- How to get element at specific position in List in C++
- Iterate over characters of a string in C++
- Difference between int (*p) and int* p?
- 3-way comparison operator (Space Ship Operator) in C++ 20
- How to generate a vector with random values in C++?
- Brief Overview & Comparison of Object-Oriented Programming from C to Java
- Relational Database from CSV Files in C
- Difference between std::set vs std::vector in C++ STL
- Vector of Maps in C++ with Examples
- Data Conversion in C++
- Bug Tracking System
- C Program to sort rows of the Matrix
- Order of execution in initializer list in C++
Improved By : DeepakTiwari2