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:
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 ).int fn(const QList<int> & list) {return list.size();}
fn(QList<int>() << 7 << 9); //works!! isn't it the same?
Bookmarks