References¶

Pointers, Seen Differently: References (1)¶

Problem

• Passing parameters by copy is expensive

• Especially when objects are large (well, a point is not so large, but you get the point)

 class point { public: float distance(point p) const { int dx = abs(_x-p._x); int dy = abs(_y-p._y); return sqrt(dx*dx+dy*dy); } }; point a(1,2); point b(2, 3); float dist = a.distance(b); Problem Parameter is a copy Solution Pass by pointer Even better: const pointer

Pointers, Seen Differently: References (2)¶

 Definition Usage class point { public: float distance(const point *p) const { int dx = abs(_x-p->_x); int dy = abs(_y-p->_y); return sqrt(dx*dx+dy*dy); } }; point a(1,2); point b(2, 3); float dist = a.distance(&b); Problem User has to take the address Pointers can easily be NULL Solution References

Pointers, Seen Differently: References (3)¶

 Definition Usage class point { public: float distance(const point &p) const { int dx = abs(_x-p._x); int dy = abs(_y-p._y); return sqrt(dx*dx+dy*dy); } }; point a(1,2); point b(2, 3); float dist = a.distance(b); Pretty, because … Looks like ordinary parameter passing Compiler takes the address ⟶ physically, a pointer is passed NULL pointer passing (nearly) impossible