A
Aguilar, James
Alright, first off, sorry for all the questions. I looked this up in the
FAQ and in B.Soustroupe's book, and I could not find the answer. It's
possible that I just don't know where to look, but please help me out
anyway.
Here's the issue. Suppose I have a function that takes a reference to an
object that represents a Point:
class Point { . . . };
void fun1(Point& c) { . . . }
and then I have another function that makes a call to fun1, but has to make
a lot of Points (and add them together) in between:
void move(Point& currentPoint, Direction d) //Direction is based on keypad
{
int tmpdy, tmpdx;
tmpdx = -((d % 3) - 1); /*d%3 will give 0, 1, or 2, -1 will give opposite
of
intended direction, - gives intended direction*/
tmpdy = ((d - 1) / 3) - 1;/*d-1/3 gives 0, 1, or 2 (9 == d.max), subtract
1
gives intended direction*/
fun1(currentPoint + Point(tmpdx, tmpdy));
}
This fails several times, as you might have guessed. The first time it
fails is because Point:perator +(Point& pt) takes a reference to a point,
and for some reason, Point(tmpdx, tmpdy) cannot be translated to a
reference, even though replacing that one line with these two causes that
problem to go away:
Point dir(tmpdx, tmpdy);
fun1(currentPoint + dir);
Now, there's still a problem because fun1 expects a reference to a point and
I'm giving it a Point (I thought they translated automatically??), so I have
to change it again to:
Point dir(tmpdx, tmpdy);
Point targ = currentPoint + dir;
fun1(targ);
And now it finally works. My question: why do I need these three lines
instead of just one?
FAQ and in B.Soustroupe's book, and I could not find the answer. It's
possible that I just don't know where to look, but please help me out
anyway.
Here's the issue. Suppose I have a function that takes a reference to an
object that represents a Point:
class Point { . . . };
void fun1(Point& c) { . . . }
and then I have another function that makes a call to fun1, but has to make
a lot of Points (and add them together) in between:
void move(Point& currentPoint, Direction d) //Direction is based on keypad
{
int tmpdy, tmpdx;
tmpdx = -((d % 3) - 1); /*d%3 will give 0, 1, or 2, -1 will give opposite
of
intended direction, - gives intended direction*/
tmpdy = ((d - 1) / 3) - 1;/*d-1/3 gives 0, 1, or 2 (9 == d.max), subtract
1
gives intended direction*/
fun1(currentPoint + Point(tmpdx, tmpdy));
}
This fails several times, as you might have guessed. The first time it
fails is because Point:perator +(Point& pt) takes a reference to a point,
and for some reason, Point(tmpdx, tmpdy) cannot be translated to a
reference, even though replacing that one line with these two causes that
problem to go away:
Point dir(tmpdx, tmpdy);
fun1(currentPoint + dir);
Now, there's still a problem because fun1 expects a reference to a point and
I'm giving it a Point (I thought they translated automatically??), so I have
to change it again to:
Point dir(tmpdx, tmpdy);
Point targ = currentPoint + dir;
fun1(targ);
And now it finally works. My question: why do I need these three lines
instead of just one?