PDA

View Full Version : a question about QVariant



calmspeaker
13th August 2009, 12:22
Hi all
In the source code of Qt, we could see the below code


return QVariant();
VS9 will report a warning:
warning C4172: returning address of local variable or temporary
We all know returning address of local var is very dangerous!
I want to know is that(return QVariant();) correct?

yogeshgokul
13th August 2009, 12:31
This is returning a name less empty object. So no probs !!

nightghost
13th August 2009, 22:16
Depends on the Signature.



// bad, return by reference a local variable
QVariant& myClass::myFunction() {
return QVariant();
}

// ok, return by copy
QVariant myClass::myFunction() {
return QVariant();
}

// Also ok, return by reference
QVariant& myClass::myFunction(QVariant& my_variant) {
return my_variant;
}


See (for example) here for a detailed expanation (http://www.learncpp.com/cpp-tutorial/74a-returning-values-by-value-reference-and-address/)

calmspeaker
14th August 2009, 02:44
This is returning a name less empty object. So no probs !!

Thank you for replying!
What do you mean by empty object?
In Qt doc:
QVariant::QVariant ()

Constructs an invalid variant.
In Qt source code:

struct Private
{
inline Private(): type(Invalid), is_shared(false), is_null(true) { data.ptr = 0; }
inline Private(const Private &other)
: data(other.data), type(other.type),
is_shared(other.is_shared), is_null(other.is_null)
{}
union Data
{
char c;
int i;
uint u;
bool b;
double d;
qlonglong ll;
qulonglong ull;
void *ptr;
PrivateShared *shared;
} data;
uint type : 30;
uint is_shared : 1;
uint is_null : 1;
};
I deduce that the QVariant() is not empty.It at least contains type of which value is Invalid!

wysota
14th August 2009, 07:42
Empty in the terms of semantics ("without any user relevant data").