PDA

View Full Version : return a QVariantList



ggdev001
25th February 2013, 09:32
Hi,

it is not ok to return a QVariantList variable from a function right?

e.g.,


QVariantList app::f()
{
QVariantList obj;
return obj;
}

because obj will go out of scope when f exits?? (ps.I understand this maybe common to all data types, just checking).

lanz
25th February 2013, 09:51
No, it's OK, because here obj is being returned by value(i.e. a copy of obj will be created and then returned).

You shouldn't do this only if value is returned by reference, for example:

QVariantList &app::f ()
{
QVariantList obj;
///WRONG!
return obj;
}

ggdev001
25th February 2013, 10:00
Yes I also think it is ok in general to return QList QMap or any other data type.

I think problematic also next to what you mentioned is if I am supposed to return a pointer from a function and I return
a pointer to a local variable (e.g., int x) that was defined inside the function.