A co-worker wrote the following code, which I am convinced is wrong.
I want to explain the problems to him, but don't know the proper term, so I cannot find references to support my position:
His code:
BSTR someString = _bstr_t(L"Hello World");
Why I think it is wrong:
I believe that _bstr_t(L"Hello World");
calls the constructor for _bstr_t
, and creates a short-lived temporary variable of that type. That temporary will be automatically deleted, and its string-space freed up, immediately after this line of code (after the semi-colon sequence point).
This will leave someString
pointing to memory that has been freed.
Questions:
What is the proper term for that call of a constructor?
Can you point to some references/terms/pages that describe the use in detail?
Is there a term for the temporary _bstr_t
object?
I guess I would call it an "anonymous, temporary variable", but I don't know if that is technically accurate.
(or maybe I'm completely wrong in my analysis.... if so, I'd love to know)
For clarification:
_bstr_t is a C++ class, commonly used by Microsoft to wrap their BSTR type, so it has constructors/destructors/operators, etc.
BSTR
is a typedef for just a WCHAR*
, so it does not have any logic. Its just a dumb-pointer.