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);
}
};
|
|
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
Solution
|
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 …
|