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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- How to iterate through a Vector without using Iterators in C++
- Code Bloating in C++ with Examples
- Implementation of lower_bound and upper_bound on Set of Pairs in C++
- Difference Between C Language and LISP Language
- Implementation of lower_bound() and upper_bound() on Map of Pairs in C++
- Pointer Arithmetics in C with Examples
- Difference between GCC and G++
- std::cerr | Error Stream Object in C++ with Examples
- Methods vs. Functions in C++ with Examples
- Difference and Similarities between PHP and C
- basic_istream::readsome() in C++ with Examples
- std::is_function template in C++ with Examples
- type_traits::is_null_pointer in C++
- std::extent() template in C++ with Examples
- basic_istream::swap() in C++ with Examples
Improved By : DeepakTiwari2