PDA

View Full Version : Return value to QList



lyucs
13th October 2009, 14:45
I have a method that returns a QList<int>.

some possibilities:
- QList is returned filled with ints. OK.
- QList returns empty, but because no ints were put in it.

but the method might result in error, and I have to return it nonetheless.
right now, I'm returning a empty QList, but its no good, since a empty QList is possible to happen without error.

I tryed returning 'NULL', but C++ won't let me.
I thought of returning a QList with the first element set to -1, but that's lame.

What can I return instead?
I'd rather not use Exceptions...

thankyou

jano_alex_es
13th October 2009, 14:56
think on an impossible value and add it to the list. If the list has only that value, it's like empty.

Boron
13th October 2009, 16:39
What about a "bool* ok" function argument?

spawn9997
13th October 2009, 19:14
IF you want to test for NULL then you might try returning a QList<int> *.


jw
sw developer

lyucs
15th October 2009, 19:45
think on an impossible value and add it to the list. If the list has only that value, it's like empty.
that's what I thought when I said about returning the first element set as -1... but, that's so... not elegant. :p


What about a "bool* ok" function argument?
I can't change any arguments, but maybe I can use that sugestion somewhere else. thanks



IF you want to test for NULL then you might try returning a QList<int> *.
if you meant

return QList<int> *;
that doesn't work. I've got nooo idea why.

thank you all, I think I'll have to rely in 'not elegant' methods XD

Lykurg
15th October 2009, 21:16
The "bool *ok" solution is the best. If you can't alter the arguments but you can alter the returned value you can use
QPair<bool, QList<int> > returnListFunction() {}

wysota
15th October 2009, 21:33
What can I return instead?

You can return an empty list and besides that have a "lastError" method in your class to determine if the last call was successful or not. Qt uses this approach in many places as an alternative to exceptions.

john_god
16th October 2009, 02:31
I use a public bool variable in the class, than, set it in the beginning of the method and change it if the error occur. After calling the method, I check the flag value.