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
Project Description: We refurbish older computer and send them back into the market with windows 7 operating system. We are presently using windows deployment services through P

Write a program consisting of two functions (plus main). The first function should read in a number and then pass this back to main. This value should then be passed into a secon

coloring of elements in matrix form inm particular matrix.the color should not match with another color.

Aim: To implement a program for following string operations: Length of String. String Concatenation. Substring (provide start index and length) Find character a

how to avoid duplicates strings in an array

The following is the formula that can be used to complete that calculation: Monthly Payment = Monthly Interest Rate / (1 - (1 + Monthly Interest Rate) -Payment Interval )) *

I need website Product section search box coding section fix Project Description: On our products section in the search box it only searches the name and title of our product

flow chart for calculating the volume of sphere

C Program for SORTING OF LONG WORD WITH DIFFERENT STRING #include stdio.h> #include conio.h> #include string.h>   void main() {           char n[50],c[25][25]

How do I allocate multidimensional arrays by new? A: There are several ways to do this, based on how flexible you wish the array sizing to be. On one acute, if you know all the