@saini oops... i answered very fast...Let me think...Destruction of this pointer. Delete operator works only for heap allocated data. If you object is created using new, then you can apply delete this, otherwise behavior is undefined delete this will not normally affect the this pointer itself, so it can still be used to call the function. This behaviour is undefined though - it might work, it might not. In general, delete this is a bad idea in C++. The only justification for using it is in some reference counted classes, and there are beter approaches to reference counting which do not require its use.
On Wed, Jun 5, 2013 at 9:21 PM, Nitish Raj <[email protected]> wrote: > When destroy called it will print garbage then it will print desired one. > Coz this pointer is self link but with different address of this pointer. > So after deletion of pointer this ... > On 5 Jun 2013 14:37, "shubham saini" <[email protected]> wrote: > >> #include<iostream> >> using namespace std; >> >> class Test >> { >> private: >> int x; >> int y; >> public: >> Test(int x = 0, int y = 0) { this->x = x; this->y = y; } >> void setX(int a) { x = a; } >> void setY(int b) { y = b; } >> void destroy() { delete this; >> cout<<"x="<<this->x<<",y= "<<this->y; >> } >> void print() { cout << "x = " << x << " y = " << y << endl; } >> }; >> >> int main() >> { >> Test *obj=new Test(); >> (*obj).setX(10); >> (*obj).setY(20); >> (*obj).destroy(); >> (*obj).print(); >> return 0; >> } >> >> i created object dynamically yet how it is still able to print values of >> x & y even after deletion of object through 'this' . >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> >> >> > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
