Is it legal for a member function to say delete this?, C/C++ Programming

Is it legal for a member function to say delete this?

A: As long as you're cautious, it's OK for an object to delete this.

Here's how I define "cautious":

You have to be absolutely 100% positive sure that this object was allocated through new (not through new[], nor through placement new, nor a local object on the stack, nor a member of another object nor a global,; but by plain ordinary new).

You have to be absolutely 100% positive ensure that your member function will be the last member function invoked on this object.

You have to be absolutely 100% positive ensure that the rest of your member function (after delete this line) doesn't touch any piece of this object (by including calling any other member functions or touching any data members).

You have to be absolutely 100% positive ensure that no one even touches the pointer itself after the delete this line. In other terms, you have to not examine this, compare this with another pointer, compare this with NULL, print this, cast this, do anything with this.

Of course the usual caveats apply in cases where your this pointer is a pointer to a base class while you don't have a virtual destructor.

 

Posted Date: 3/19/2013 8:50:02 AM | Location : United States







Related Discussions:- Is it legal for a member function to say delete this?, Assignment Help, Ask Question on Is it legal for a member function to say delete this?, Get Answer, Expert's Help, Is it legal for a member function to say delete this? Discussions

Write discussion on Is it legal for a member function to say delete this?
Your posts are moderated
Related Questions

#include #include #include #include #include class Employee { private: char *Name; //Set them as pointers... int IdNumber; char *Department; char *Position; public: voi

.Create a Csharp solution that will show a maze in the user interface and will show a mouse walking through the maze attempting to locate the exit door (the cheese).

Consider text comprised of sentences and sentences comprised of words. Words in a sentence will be space delimited. Given a text and K strings, task is to find out the number valid

Classify computer system according to capacity. How they are different from computers according to the classification of technology. Provide comparative study also.

For this program you will add and test 2 new member functions to the IntSLList class posted on the website. The two member functions are: insertByPosn(int el, int pos) Assuming t

Program for Hangman Problem   #include   #include   #include   #include   #include   static void playGame();   static void printMistakes(int n);   st

Create a program WordCount1Main.java doing the following:  For each word in the le word.txt { Create an object of the class Word { Add the object to a set of the type java.uti

Write a function that has four unsigned char parameters, combines the four one-byte integer values into an unsigned integer, and returns the unsigned integer. When the four one

Write a program to calculate the average of ten numbers       #include stdio.h     /* Only this header is needed since only IO are used */     void main()     {          char