Skip to Content
Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions
book

Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions

by Herb Sutter
November 1999
Intermediate to advanced
240 pages
5h 22m
English
Addison-Wesley Professional
Content preview from Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions

Chapter 41. Object Lifetimes—Part 2

Difficulty: 6

Following from Item 40, this issue considers a C++ idiom that's frequently recommended—but often dangerously wrong.

Critique the following “anti-idiom” (shown as commonly presented).

T& T::operator=( const T& other ) 
{
  if( this != &other )
  {
    this->~T();
    new (this) T(other);
  }
  return *this;
}
  1. What legitimate goal does it try to achieve? Correct any coding flaws in this version.

  2. Even with any flaws corrected, is this idiom safe? Explain. If not, how else should the programmer achieve the intended results?

(See also Item 40.)

Solution

Solution

This idiom[3] is frequently recommended, and it appears as an example in ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions

Exceptional C++ Style 40 New Engineering Puzzles, Programming Problems, and Solutions

Herb Sutter

Publisher Resources

ISBN: 0201615622Purchase book