Variable life cycle

This is a discussion on Variable life cycle within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, In the sample, I am wondering what is the life cycle of variable b_? Could we access variable ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you compile it, you should have your answer.
    1 is correct.

    _b(...) doesn't declare anything. You need to specify a type to declare it, like this:
    B _b;

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If b_ failed to be constructed, I don't see how you can access it.

    The article seems to say that if you have multiple member objects and one of them fails to be constructed, you cannot access any of them in the catch block and the exception will be automatically rethrown.
    Last edited by anon; 12-26-2007 at 05:06 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The object is in scope, so you can access it; however, the object may or may not have been constructed, so it definitely isn't safe to access it. Accessing an uninitialized object causes undefined behaviour.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks cpjust,


    It can compile. :-)

    Quote Originally Posted by cpjust View Post
    If you compile it, you should have your answer.
    1 is correct.

    _b(...) doesn't declare anything. You need to specify a type to declare it, like this:
    B _b;

    regards,
    George

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    I agree, thanks anon!


    Quote Originally Posted by anon View Post
    If b_ failed to be constructed, I don't see how you can access it.

    The article seems to say that if you have multiple member objects and one of them fails to be constructed, you cannot access any of them in the catch block and the exception will be automatically rethrown.

    regards,
    George

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    I agree, thanks CornedBee!


    Quote Originally Posted by CornedBee View Post
    The object is in scope, so you can access it; however, the object may or may not have been constructed, so it definitely isn't safe to access it. Accessing an uninitialized object causes undefined behaviour.

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. life cycle of exception object
    By George2 in forum C++ Programming
    Replies: 43
    Last Post: 02-13-2008, 07:50 AM
  2. Artificial Life: Where to Start?
    By Mr.Sellars in forum General AI Programming
    Replies: 11
    Last Post: 09-22-2007, 03:03 AM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 01:30 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21