Results 1 to 17 of 17

Thread: Qt initializer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 342 Times in 324 Posts

    Default Re: Qt initializer

    Reference is just like a pointer, it needs a valid, existing object to point to. In your case (1) the temporary list gets deleted soon after its created, reference does not "copy" the content of temporary objects. Second case is ok, reference points to an existing object.
    Why do you want to use references anyway ?
    ---
    edit:
    int fn(const QList<int> & list) {return list.size();}

    fn(QList<int>() << 7 << 9); //works!! isn't it the same?
    The argument passed to function remains valid until the function returns, note that you cannot use the passed argument after the fn() finishes, because it no longer exists. In your first code snippet the argument passed to reference initializer gets deleted too, but you still want to use it later ( by holding a reference to it ).
    Last edited by stampede; 20th September 2011 at 11:11.

Similar Threads

  1. Replies: 1
    Last Post: 4th September 2008, 14:55
  2. expected initializer before »)« token
    By Holy in forum Qt Programming
    Replies: 2
    Last Post: 24th May 2008, 16:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.