PDA

View Full Version : Function arguments - how to get a reference



olidem
3rd March 2009, 10:14
Hi,

I have a basic general question.

Most QT functions are called by reference, eg.



QRectF::contains(QPointF & pt)


On the other hand, many functions return eg. just a QPointF,



QPointF QGraphicsItem::mapToScene()


How can I combine these arguments in an elegant way without havin to create an intermediate local variable ?

Tanks for your hints in advance!

Olli

caduel
3rd March 2009, 11:30
those function more often use const references...
for const references you can just pass the (temporary) value:




QRectF rect=...;
QPointF pt=...;
QGraphicsItem *item=...;
rect.contains(item->mapToScene(pt));

You can't do that for non-const references, though.

HTH

^NyAw^
3rd March 2009, 11:31
Hi,

You don't need to use an internal variable:

"mapToScene" returns a QPointF and you can pass it to "contains" method directly.
QRect::contains(QPonitF & pt) tells you that it gets the QPonitF "pt" as a reference, so it not will copy the object. It's like a pointer to the object