A question on ListView_SetItemText macro for setting text of items in ListView Windows control.
Why does the following code works (in a sense that the text is displayed in the ListView)
std::string strNumber = NumberToString(number);
ListView_SetItemText( hListView, iItemIndex, iSubitemIndex, (LPSTR)strNumber.c_str() );
while a direct call doesn’t
ListView_SetItemText( hListView, iItemIndex, iSubitemIndex, (LPSTR)NumberToString(number).c_str() );
where
std::string NumberToString ( double Number )
{
std::ostringstream ss;
ss << Number;
return ss.str();
}
Many thanks
Here’s the macro definition (in
commctrl.h):This expands to:
The
NumberToStringfunction returns a temporarystd::string, which is deleted before theSNDMSGcall. So_lvi.pszTextpoints to thin air. (Your code would be perfectly safe ifListView_SetItemTextwere a genuine function call.)