Variable life cycle - c++
This is a discussion on Variable life cycle - c++ ; Hello everyone,
In the sample, I am wondering what is the life cycle of variable b_?
Could we access variable b_ in catch block?
I have this confusion is because,
1. I think b_ is member variable, and we should ...
-
Variable life cycle
Hello everyone,
In the sample, I am wondering what is the life cycle of variable b_?
Could we access variable b_ in catch block?
I have this confusion is because,
1. I think b_ is member variable, and we should be able to access it
anywhere in the class itself, so we can access b_ in catch block;
2. I think b_ is declared and initialized in try {} block, and catch
exceeds the {} of try, so we can not access b_ in catch block.
Which option is correct?
http://www.gotw.ca/gotw/066.htm
Code:
class C:
{
B b_;
C::C()
try
: b_( /*...*/ )
{
}
catch( ... )
{
// can we access _b here?
}
};
thanks in advance,
George
-
Re: Variable life cycle
George2 wrote:
:: Hello everyone,
::
::
:: In the sample, I am wondering what is the life cycle of variable
:: b_? Could we access variable b_ in catch block?
::
:: I have this confusion is because,
::
:: 1. I think b_ is member variable, and we should be able to access
:: it anywhere in the class itself, so we can access b_ in catch
:: block;
::
:: 2. I think b_ is declared and initialized in try {} block, and
:: catch exceeds the {} of try, so we can not access b_ in catch
:: block.
::
:: Which option is correct?
::
:: http://www.gotw.ca/gotw/066.htm
::
:: Code:
:: class C:
:: {
:: B b_;
::
:: C::C()
:: try
:: : b_( /*...*/ )
:: {
:: }
:: catch( ... )
:: {
:: // can we access _b here?
:: }
::
:: };
:: ::
It think Herb explains it very well in the article - if C's
constructor fails, there is no C object. What are you to access?
This is a very unusual construct, that has hardly any use at all. In
fact, most compilers hasn't bothered to implement it.
Bo Persson