Variable life cycle

G

George2

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
 
B

Bo Persson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top