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
This is output I got
klkl destructor called 2 177 Hello, World! destructor called 2 -1073743372 destructor called 1 1108517584
while calling void f (play p) from main , you are calling compiler generated copy constructor and an object p is created, this calls the destructor once f() gets over.
And the other two destructors are called before returning from main.
Seems like there is some compiler issue, hence you are getting 4 destructors.
Someone throw some light ....................
--Tapesh
--- Abhijeet D Mhatre abhijeetmhatre@gmx.net wrote:
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
--
===== ********************************************* It doesn't make a difference what temperature a room is, it's always room temperature.
--Steven Wright *********************************************
__________________________________ Do you Yahoo!? The New Yahoo! Search - Faster. Easier. Bingo. http://search.yahoo.com
On Tue, 6 May 2003, Abhijeet D Mhatre wrote:
I have a doubt in C++. when I pass an object to a function, the destructor gets called twice.
Are you passing an object or a pointer to the object? You should try and understand what call by value and call by reference means. Then, you will understand how many objects are created and when.
Abhijeet D Mhatre wrote:
I have a doubt in C++. when I pass an object to a function, the destructor gets called twice.
<snip>
<disclaimer> Haven't done C++ for ages now, so don't take my word for it. </disclaimer>
Use a copy constructor:
play(const play& copy_me) { i = copy_me.i; j = copy_me.j; }
-Manish