first:
LPCTSTR asdfsdf = (LPCTSTR)(_bstr_t)v;
printf("%s\n", asdfsdf);
second:
printf("%s\n", (LPCTSTR)(_bstr_t)v);
they are the same, but the first condition causes unreadable code
why?
first:
LPCTSTR asdfsdf = (LPCTSTR)(_bstr_t)v;
printf("%s\n", asdfsdf);
second:
printf("%s\n", (LPCTSTR)(_bstr_t)v);
they are the same, but the first condition causes unreadable code
why?
The _bstr_t
class encapsulates a BSTR inside a C++ class. In your first instance:
LPCTSTR asdfsdf = (LPCTSTR)(_bstr_t)v;
you are creating a _bstr_t
object, extracting the LPCTSTR
out of it, but then the temporary _bstr_t
object gets destructed. Whatever asdfsdf
pointed to is now deallocated and can no longer be used.
In your second example
printf("%s\n", (LPCTSTR)(_bstr_t)v);
the temporary _bstr_t
object is not destructed until after the printf()
is called, so there is no problem using the LPCTSTR
value.
v
is. The problem is that the _bstr_t
temporary object creates a copy of the data from v
when it is constructed, and deallocates that copy when the temporary is destructed. –
Pocked © 2022 - 2024 — McMap. All rights reserved.