_bstr_t to char *, awesome result

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?

+4
source share
1 answer

_bstr_t class encapsulates the BSTR inside the C ++ class. In the first case:

 LPCTSTR asdfsdf = (LPCTSTR)(_bstr_t)v; 

you create the _bstr_t object by extracting _bstr_t from it, but then the temporary _bstr_t object is _bstr_t . Regardless of what is specified asdfsdf , is now freed and can no longer be used.

In your second example

 printf("%s\n", (LPCTSTR)(_bstr_t)v); 

the temporary _bstr_t object _bstr_t not destroyed until printf() is called, so there is no problem using the value of LPCTSTR .

+5
source

All Articles