[Qt-interest] Qt code compiles fine on mscv9.0 but failed with gcc

Thiago Macieira thiago at kde.org
Wed Jan 26 17:17:27 CET 2011


Em quarta-feira, 26 de janeiro de 2011, às 18:39:07, Constantin Makshin 
escreveu:
> I guess that in MSVC, when you pass a temporary object as a non-const
> reference and then the function modifies this referenced object, it
> modifies the temporary object.
[snip]
> then the only difference from the first example is that any changes are lost
> when the temporary object is destroyed, i.e. when someFunc() finishes its
> work.
> 
> While this behavior may be wrong from C++ point of view, it has some sense
> IMHO.

No, it's completely wrong. It violates RAII and it enables this:

struct Foo
{
    QString &s;
    Foo(QString &s) : s(s) {}
};

void function()
{
    Foo f(QString("abc"));
    f.s = "def";
}

The Foo::s member of f was initialised as a non-const lvalue ref to a 
temporary. The temporary was subsequently destroyed at the end of that 
statement. But then we proceeded to use it on the next statement, 
dereferencing a dangling pointer (s.QString::d).

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110126/b1cddf3f/attachment.bin 


More information about the Qt-interest-old mailing list