I l@ve RuBoard previous section next section

Item 40. Object Lifetimes—Part 1

Difficulty: 5

"To be, or not to be…" When does an object actually exist? This problem considers when an object is safe to use.

Critique the following code fragment.

void f() 
{
  T t(1);
  T& rt = t;
  //--- #1: do something with t or rt ---
  t.~T();
  new (&t) T(2);
  //--- #2: do something with t or rt ---
}// t is destroyed again

Is the code in block #2 safe and/or legal? Explain.

I l@ve RuBoard previous section next section