What happens if you make call "delete this;"?, C/C++ Programming

The code contains two built-in pitfalls. First one, if it executes in a member function for an static, extern or automatic object, the program will possibly crash as soon as the delete statement executes. There is not any portable way for an object to tell that it was instantiated on the heap, thus the class cannot assert that its object is correctly instantiated. Second one, while an object commits suicide this way, using program may not know about its demise. So far as the instantiating program is concerned, the object remains in scope & continues to exist although the object did itself in. Usually Subsequent dereferencing of the pointer can & does lead to disaster.

You must never do this. As compiler does not know whether object was allocated onto the stack or on the heap, "delete this" could cause a tragedy.

 

Posted Date: 3/16/2013 2:58:47 AM | Location : United States







Related Discussions:- What happens if you make call "delete this;"?, Assignment Help, Ask Question on What happens if you make call "delete this;"?, Get Answer, Expert's Help, What happens if you make call "delete this;"? Discussions

Write discussion on What happens if you make call "delete this;"?
Your posts are moderated
Related Questions
c program for padovan string   Padovan series are positive integers obtained by the following process: The Padovan series is the sequence of integers P(n) defined by the

Project Description: Design and prepare software that can navigate a mobile robot using SLAM technique using vision sensor (camera). Skills required are C Programming, Engine

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

Explain the terms- Substitutability and Extensibility Substitutability - The objects of a properly derived class can be safely and easily substituted for an object of its

P r i va t e Member Functions: A private member functions can be called by the members of the same class.  Consider the following example.   c l a ss sample

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

Explain the class invariant. - It's a condition that ensures correct working of a class and defines all the valid states for an object. - When an object is created class inv

#include stdio.h   struct  complex   {   float real;   float imag;   };   struct complex complexadd(struct complex,struct  complex);   void main()     {          Date: 26

program to convert string from super ascii string with minimum cost

Write a recursive function recursiveMin that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element in the array. The