[Qt-interest] transforming QString into char *

Tim W timpie.w at gmail.com
Tue Mar 24 09:56:34 CET 2009


On Mon, Mar 23, 2009 at 5:33 PM, Volker <volker at openbios.org> wrote:

> Am Montag 23 März 2009 15:32:43 schrieb Bob Hood:
> >
> >     const char *string_data = myString.toLatin1().constData();
> Is this really safe?
> There is a temporary object of type QByteArray involved, which IMHO is
> destroyed once the statement is executed.
>

You are absolutely right.  It should be:

const QByteArray byteArray(myString.toLatin1());
const char *string_data = byteArray.constData();




>
> On the other hand, a statement like this
> someFunction( myString.toLatin1().constData() );
> should be ok, as the temp objects remain valid until the whole statement is
> finished (provided someFunction does not remember the passed pointer)
>


You are right once again as long as somefunction doesn't store a pointer to
the string data.

Cheers,
Tim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090324/1e9db08e/attachment.html 


More information about the Qt-interest-old mailing list