Hi,
I the below code i could see 2 constructors and 3 destructors being called....i want to know why....can anyone help me in knowing this???
-
class e
-
{
-
-
public:
-
e()
-
{
-
cout<<"Contructor\n";
-
}
-
~e()
-
{
-
cout<<"Destructor\n";
-
}
-
void f(e e1)
-
{
-
cout<<"In e\n";
-
}
-
};
-
int main(int argc, char* argv[])
-
{
-
{
-
e ee;
-
e ee1;
-
ee.f(ee1);
-
//printf("Hello World!\n");
-
//cout<<"Raghava"<<endl;
-
}
-
int t;
-
cin>>t;
-
return 0;
-
}
-
in
Use Code Tags while you do Post according to Posting Guidelines.
Look let me explain.
Constructors for object initialization, means the memory allocation for that Object. And destructor is for when Object is about to destroy.
And whether memory is more essential so Object destruction should be handled carefully.
Generally if your Object is not created using Pointer then your Run Time System will decided when your Object goes out of Life-Time Scope means then it is about to destroy.
Here your see two objects are created but look at this line carefully.
-
void f(e e1)//Here an Object is Copied using Copy Constructor.
-
//If the line is ............. void f(e &e1), then no Object creation would be there.
-
That's why three times the Object destruction happens.
Kind regards,
Dmjpro.