如果我有一個wstringstream
,並且我想將它的.str()
數據作爲LPCWSTR,我該怎麼做?C++:從wstringstream獲取LPCWSTR?
1
A
回答
12
你可以做wstringstream.str().c_str()
as DeadMG writes。但是,該調用的結果僅在表達式的生命週期結束時纔有效,這是該表達式的一部分。
具體地,該
const LPCWSTR p = wss.str().c_str();
f(p); // kaboom!
將不起作用,因爲wstringstream.str()
返回一個臨時對象和.c_str()
返回一個指針到該對象,並在分配的結束該臨時對象將被破壞。
你可以做的,而不是要麼
f(wss.str().c_str()); // fine if f() doesn't try to keep the pointer
或
const std::wstring& wstr = wss.str(); // extends lifetime of temporary
const LPCWSTR p = wstr.c_str();
f(p); // fine, too
因爲綁定到const
引用臨時對象都會有自己的壽命延長了參考的一生。
0
wstringstream.str().c_str();
相關問題
- 1. C++:不能使用std :: wstringstream
- 2. 麻煩與wstringstream
- 3. 從LPCWSTR轉換爲LPCSTR
- 4. 從std :: wstring Asisgn LPCWSTR數組
- 5. 從函數返回LPCWSTR?
- 6. 將QString轉換爲LPCWSTR qt C++
- 7. 'System :: String ^'到'LPCWSTR'
- 8. 幫助上LPCWSTR
- 9. WCHAR到LPCWSTR
- 10. 不能從'QString'轉換爲'LPCWSTR'在Qt
- 11. 不能轉換從 '字符' 到 'LPCWSTR'
- 12. 從DataView獲取價值C#
- 13. C++從PHP獲取變量?
- 14. 獲取從對象C#
- 15. 獲取從RichTextBox的在C#
- 16. C#獲取從ArrayList中
- 17. 如何從C#獲取ExecutionPolicy?
- 18. 獲取osk.exe從C#運行#
- 19. C#:從IConvertible獲取Bytearray
- 20. 從mongodb c獲取對象#
- 21. 從C#獲取JSON值#
- 22. c#從gridview獲取值
- 23. 從AssocQueryString獲取AppName c#
- 24. C#獲取行從DataGridView
- 25. C++傳遞一個wstringstream到另一個功能
- 26. sprintf和LPCWSTR變量
- 27. 如何從C/C++獲取virt大小?
- 28. 觸發由C++ C#的事件和WinCE7設備上宣佈LPCWSTR
- 29. 如何使用類wstringstream變量?
- 30. 將LPCWSTR投射到LPCSTR
或者,如果分配給一個常量引用,則引用的生存期爲 – 2010-06-07 17:41:32
@Greg:我剛剛正在編寫這個過程。 ':'' – sbi 2010-06-07 17:44:36
我認爲你的第二個代碼不好,因爲C++可以在調用'f'之前銷燬臨時文件。這真的發生在我身上一次!所以你不能存儲'c_str()'或'data()'的結果。 – Philipp 2010-06-07 19:47:33