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

Assignment Help:

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.

 


Related Discussions:- Is it legal for a member function to say delete this?

Wap to print series from 1 to 10 & find its square and cube, WAP TO PRINT S...

WAP TO PRINT SERIES FROM 1 TO 10 & FIND ITS SQUARE AND CUBE # include stdio.h> # include conio.h> # include math.h>   void main () { int a=1,sqr=0,cube=0;

Explain the different types of errors in php, Explain the different types o...

Explain the different types of errors in PHP. Warnings, Notices and Fatal errors are the types of errors in PHP Notices: Notices signifies non-critical errors, i.e. ac

Procedure to compute recursive and iterative process, Consider the followin...

Consider the following mathematical function: (a) Write a procedure that computes f by means of a recursive process (b) Write a procedure that computes f by means of an

Circular linked list assignment, need an expert programmer to finish coding...

need an expert programmer to finish coding the requirements from the assignment withen 4 hrs

#decode the code, #Smugglers are becoming very smart day by day. Now they h...

#Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new techn

Lexicographically preceding permutation, Respected Sir, I want the above wr...

Respected Sir, I want the above written text in a C program

How can i provide printing for whole hierarchy of classes?, A: Provide a fr...

A: Provide a friend operator class Base { public: friend std::ostream& operator ... protected: virtual void printOn(std::ostream& o) const; }; inline std::ostr

Stone Game - Remove Last, #include int main() { int T; ...

#include int main() { int T; int N; int i; scanf("%d",&T)

Super ASCII stringchecker, In the Byteland country a string "s" is said to ...

In the Byteland country a string "s" is said to super ascii string if and only if count of each charecter in the string is equal to its ascci value in the byteland country ascii co

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd