Additionally to the answers 1, 2, 3 and GotW88, assume the following methods
QString createString()
{
return QString("foobar");
}
const QString& getString()
{
return createString();
}
This will yield the famous "warning C4172: returning address of local variable or temporary" with VS2013.
Now if i changed the second method to
const QString& getString()
{
const QString& binder = createString();
return binder;
}
Which does not report an error anymore. Is this a safe way to fix the warning without changing the signature of the API? Why does this work?
It doesn't work. That way you simply suppress the warning by making the situation harder to analyze. The behavior is still undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With