HI Friends I have a doubt in C++. when I pass an object to a function, the destructor gets called twice. This behaviour hasn't been mentioned in any books that I have read. Can someone please explain why this happens.
In the below example the destructor for p1 gets called twice when it is passed as to the function f.
_______________________C++ Code Start____________________________________ #include <iostream.h> #include <stdlib.h> class play { public: int i ; int j; play(int m){i=m; } ~play() {cout << "destructor called " << i <<" " << j << endl; } void f (play p) { p.j = 177; cout << " klkl " << endl; }
};
int main(int argc, char *argv[]) { play p(1), p1(2); p.f(p1); cout << "Hello, World!" << endl;
return EXIT_SUCCESS; }
_______________________C++ Code End_____________________________________
______________________Output Start__________________________________ klkl destructor called 2 177 destructor called 2 134514273 Hello, World! destructor called 2 134514273 destructor called 1 134519308
Press Enter to continue! _________________Output End_____________________________________
Regards Abhijeet D Mhatre
Email: abhijeetmhatre@gmx.net