By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

References and function calls

P: n/a
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::operator +(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?
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Aguilar, James wrote:
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::operator +(Point& pt) takes a reference to a point,
And it should take a reference to a _const_ Point (and be 'const' itself):

Point Point::operator +(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
Jul 22 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:bY***************@newsread1.dllstx09.us.to.ve rio.net...
Aguilar, James wrote:

[snip]


Fascinating. Thanks, victor. I'll do that.

James
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.