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

Question: (a) A bank has four types of accounts with annual interest rates as follows: Savings 8% Savings with Cheque Book 5% Current 4% Fixed deposits 10% Using

Ravi is a newbie to the programming and while learning the programming language he came to know the following rules:

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Define the C Preprocessor? Preprocessor' is a translation stage that is applied to your source code before the compiler proper gets its hands on it usually the preprocessor per

This is a test program that tests the Student and ITECH7603Class classes.     In this assignment you are provided with three input text files associated with this program:

Custom Scans and Alerts Think or Swim Project Description: I am seeking somebody to make me several Custom Scans and Alerts on Thinkorswim TOS Skills required are C++ Prog

Describe Processing an array? The Single operations which involve complete arrays are not permitted in C language. therefore if a and b are similar arrays (for example same dim

Define the Processing of a Structure? The members of structure are typically processed individually as separate entities. So we must be able to access the individual structure