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
C Program for Palindrome Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Dynamic Initialization of objects: It is initializing the objects by passing the valued to the constructor from the user input or other means.   Through cin operator a value

Damjibhai and Shamjibhai are two jeweler friends. They decide to play a simple game. The game comprises of removing the jewels for polishing, turn by turn. Once a jewel is removed

real time application of b tree

#include stdio.h #include math.h float discharge(float,float); void main()   {   char prompt;      float time,out,cr;   cr = 100e-6;   for (time = 0;time   {   /* call th

A small shop sells 280 different items. Every item is identified by a 3 - digit code. All items which start with a zero (0) are cards, all items which start with a one (1) are swee

need some help with finishing a program

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Introduction to I/O interfacing, discuss microcontrollers applications in industry. Detailed product design specifications that have sections on both hardware and software inclu

(a) Write a procedure called (mult x y) that multiplies two numbers x and y in a recursive manner using successive addition. Specifically, note that a x b = a + a + .... + a (b tim