Wednesday, 28 August 2013

Function returning an object by value or nothing if object doesn't exists

Function returning an object by value or nothing if object doesn't exists

What would be the best signature of a function returning a class object by
value or something special if object does not exists?
For function returning more than one object, function prototype is
QList<X> getSomeXOrNothing();
it returns list of objects if there are objects, or empty list if there is
no objects, which fully suits my needs and is convenient to work with.
Now, I know that function can only return single object or nothing, what
would be the best signature? Returning a raw pointer is not desirable
since it has potential for error leaks.
I'm debating between returning shared pointer or still returning a list
with a single value:
QList<X> getSingleXOrNothing();
QSharedPointer<X> getSingleXOrNothing();
but I am wondering if there are any better ways to do that - shared
pointer adds unnecessary overhead, while returning a list is controversial
since I know list can contain only one object.

No comments:

Post a Comment