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(a)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(a)gmx.net
--
http://mm.ilug-bom.org.in/mailman/listinfo/linuxers
=====
*********************************************
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