Dec 2, 2013

[C++][NOTE][ORIGINAL] const ref to temporary R-Value class without virtual destructor trick

Even though destructor is not virtual, when out-of-scope happens, base will release reference to R-Value, thus will release R-Value, i.e Ret_R_Value(); , by calling Derived's destructor.
 

struct Base
{
  ~Base()
  {
  }
};


struct Derived : public Base
{
  ~Derived()
  {
  }
}


Derived Ret_R_Value()
{
  return Derived();
}


int main()
{
  const Base& base = Ret_R_Value();
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.