Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


What will happen if when say delete this ?

Answers were Sorted based on User's Feedback



What will happen if when say delete this ?..

Answer / mms zubeir

There are two scenarios:

1. Using "delete this;" in destructor

Here the call will be a recursive call to the destructor
infinitely. So the program hangs here.

2. Using "delete this;" in other members of a class
including constructor.

In this case, the call is a sucide call since the object
tries to delete itself which is nothing but sitting in a
space and destroy that space itself. That is definitely a
memory crash.

Is This Answer Correct ?    13 Yes 4 No

What will happen if when say delete this ?..

Answer / hidden prairie

delete this will call the destructor, but should not be
called in the destructor but in any other member function.

Is This Answer Correct ?    8 Yes 0 No

What will happen if when say delete this ?..

Answer / hitout

There are different cases to this
1) If the memory is allocated in stack, delete this should
not be called since it calls destructor twice.
2) delete this can be called in all functions except
destructor if the memory is allocated on heap(ie if new
operator is used and the object is not explicitly deleted).

3) Delete this in destructor will always cause an infinite loop.

Is This Answer Correct ?    6 Yes 1 No

What will happen if when say delete this ?..

Answer / hemant

There are two scenarios:

1. Using "delete this;" in destructor

Here the call will be a recursive call to the destructor
infinitely. So the program hangs here.

2. Using "delete this;" in other members of a class
including constructor.

In this case, the call is a sucide call since the object
tries to delete itself which is nothing but sitting in a
space and destroy that space itself. That is definitely a
memory crash.

Is This Answer Correct ?    4 Yes 2 No

What will happen if when say delete this ?..

Answer / priya

Using delete this in destructor will lead to recursive loop
which will lead to Stack overflow...so avoid it over
here...however there are few times where your code with
delete this will just work fine..like in the usage of
garbage colletors in C++.Chk the below code...which works
with no issues:
class temp
{
public:
temp(){std::cout<<"Constructor"<<std::endl;}
~temp(){std::cout<<"Destructor"<<std::endl;}
void destroy() {std::cout<<"In class
temp"<<std::endl;delete this;}

};
int main()
{
temp *t = new temp;
t->destroy();
return 0;
}

Is This Answer Correct ?    2 Yes 0 No

What will happen if when say delete this ?..

Answer / sudhir

delete this will not work because 'this' is not available to
global functions .this is only available to class member
functions.

Is This Answer Correct ?    0 Yes 3 No

What will happen if when say delete this ?..

Answer / dee

delete "this" lead in calling the destructor twice.
"this" poanter is related to object and the destructor is
called when the object goes out of scope.

Is This Answer Correct ?    2 Yes 6 No

Post New Answer

More C++ General Interview Questions

write the code that display the format just like 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 6 5 4 3 2 1

5 Answers  


What is the Diffrence between a "assignment operator" and a "copy constructor"?

3 Answers   Wipro,


What is a conversion constructor?

1 Answers  


Explain the register storage classes in c++.

0 Answers  


There is a array of 99 cells and we have to enter 1-100 elements in it , no two elements would repeat , so the is one no. missing because 99 cells and 1-100 nos. so we had to implement a function to find that missing no.

2 Answers   Nagarro,


what is pulse code modulation?

2 Answers   Wipro,


What are default parameters? How are they evaluated in c++ function?

0 Answers  


#include<iostream.h> void main() { class x { public: int func(int) { cout<<"cool"; return 1; } } }

1 Answers   Infosys,


How many namespaces are there in c++?

0 Answers  


how to find the maximum of 10 numbers ?

5 Answers  


What is the difference between strcpy() and strncpy()?

0 Answers  


How to get the current position of the file pointer?

0 Answers  


Categories