[Qt-interest] Weird compiler error with QString(QChar(value))
Nikos Chantziaras
realnc at arcor.de
Wed May 5 22:03:14 CEST 2010
That's why I hold a reference to it in the first place :) C++
guarantees that the temporary won't be destroyed until the reference
gets out of scope. This a crucial difference between references and
pointers; a pointer would just point to the temporary. A reference to
the temporary *is* the temporary.
On 05/05/2010 10:56 PM, Constantin Makshin wrote:
> Yes, holding a pointer/reference to an object to avoid unnecessary
> copies is a good idea, but not when the object lives longer than the
> pointer/reference to it. QString::toUtf8() creates a temporary
> QByteArray with UTF-8 representation of the string and returns it, and
> you keep a reference to that temporary object which can be destroyed at
> any moment, leaving your reference pointing to an invalid memory location.
>
> I'm pretty sure your idea of keeping a reference to a temporary object
> is likely to cause problems.
>
> On Wed, 05 May 2010 23:38:43 +0400, Nikos Chantziaras <realnc at arcor.de>
> wrote:
>> On 05/05/2010 10:28 PM, Constantin Makshin wrote:
>>> This isn't directly related to your question, but QString::toUtf8()
>>> returns a QByteArray object, not a reference.
>>
>> I'm holding a reference to it. This as old habit of mine in order to
>> avoid temporaries (for optimization purposes.) The compiler's optimizer
>> might or might not do that anyway, I'm not really sure, but it simply
>> stuck with me to this day.
>
More information about the Qt-interest-old
mailing list