[Qt-interest] Weird compiler error with QString(QChar(value))
Constantin Makshin
cmakshin at gmail.com
Wed May 5 21:56:13 CEST 2010
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.
--
Constantin Makshin
More information about the Qt-interest-old
mailing list