prohibiting instantiation as a temporary object (C++)

Posted by SuperElectric on Stack Overflow See other posts from Stack Overflow or by SuperElectric
Published on 2011-01-03T22:26:31Z Indexed on 2011/01/03 22:54 UTC
Read the original article Hit count: 294

I like using sentry classes in c++, but I seem to have a mental affliction that results in repeatedly writing bugs like the following:

{
  MySentryClass(arg);
  // ... other code
}

Needless to say, this fails because the sentry dies immediately after creation, rather than at the end of the scope, as intended. Is there some way to prevent MySentryClass from being instantiated as a temporary, so that the above code either fails to compile, or at least aborts with an error message at runtime?

© Stack Overflow or respective owner

Related posts about c++

Related posts about temporary