1

Possible Duplicate:
How to disallow temporaries in C++

There are times when I meant to write something like

{
    MyClass anInstance( param );
    // other code...
}

but actually wrote

{
    MyClass( param );
    // other code...
}

As a result, the destructor was called immediately after the destructor, instead of at the end of the scope. Is there any way to write the class to make this error produce a compile error? Failing that, maybe an optional warning I can turn on? (I'm using Clang on Mac OS X.)

Community
  • 1
  • 1
JWWalker
  • 22,385
  • 6
  • 55
  • 76
  • I'm sure there are solutions, but this just comes down to writing what you mean, like `=` instead of `==`. It happens, but the rate at which it happens should dramatically decrease each time you do it, to the point where you don't make that mistake any longer. (I can't think of the last time I did `=` instead of `==` for example.) When it does happen, your debugger should spot it quickly. So investing in a solution to this is probably uglier than just continuing on. – GManNickG Nov 02 '12 at 19:38
  • (Case in point, look at the atrocious solutions in the duplicate question!) – GManNickG Nov 02 '12 at 19:40

0 Answers0