References and function calls

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::eek: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?
 
V

Victor Bazarov

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::eek:perator +(Point& pt) takes a reference to a point,

And it should take a reference to a _const_ Point (and be 'const' itself):

Point Point::eek:perator +(Point const &pt) const ...
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?

Declare your function to accept a reference to _const_ Point:

void fun1(Point const& c) { . . . }

A non-const reference cannot be bound to a temporary. The temporary is
created when you return something from a function by value. The same
thing happens when operator+ is called.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top