V
Vijay
Hi All,
I am not able to figure out what exactly happening in below code. what
is control flow. Can anyone clear my confusion?
Code:
class A
{
public:
A(){cout<<"In Constructor\n";}
//// A(const A&){cout<<"In Copy Constructor\n";} // if we uncomment
this, we see different //output .
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A &a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Destructor
In Catch
In Destructor
Question 1. I don't know why two times destructor has been called. I
understand, since i am using reference, so there would not be any new
object. then why two times destructor got called.
Question 2.
class A
{
public:
A(){cout<<"In Constructor\n";}
A(const A&){cout<<"In Copy Constructor\n";}
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Copy Constructor
In Catch
In Destructor
In Destructor
Why object created by throw A() has not been deleted while exiting try
block in above code?
thanks in advance.
Regards,
I am not able to figure out what exactly happening in below code. what
is control flow. Can anyone clear my confusion?
Code:
class A
{
public:
A(){cout<<"In Constructor\n";}
//// A(const A&){cout<<"In Copy Constructor\n";} // if we uncomment
this, we see different //output .
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A &a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Destructor
In Catch
In Destructor
Question 1. I don't know why two times destructor has been called. I
understand, since i am using reference, so there would not be any new
object. then why two times destructor got called.
Question 2.
class A
{
public:
A(){cout<<"In Constructor\n";}
A(const A&){cout<<"In Copy Constructor\n";}
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Copy Constructor
In Catch
In Destructor
In Destructor
Why object created by throw A() has not been deleted while exiting try
block in above code?
thanks in advance.
Regards,