Expression
OutputDebugString(file.path().c_str())
and
OutputDebugString(file.c_str())
similar to what they effectively call the c_str()
method on a temporary std::string
object and try to use the result of this call. The first calls it directly as a subexpression of file.path().c_str()
. The second does this more implicitly: inside the FileReference::c_str()
method.
In the first case, the temporary std::string
object is explicitly created by calling file.path()
as an integral part of the whole expression. In accordance with the rules of the language, the lifetime of this temporary object extends to the end of the whole expression, therefore the temporary and result of the call to c_str()
remain valid.
In the second case, a temporary object std::string
is created inside the FileReference::c_str()
method. This temporary object is destroyed when this method returns, which means that FileReference::c_str()
returns a pointer to "dead" data. This is the reason for the "junk" information.
source share