Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Returning references (http://www.velocityreviews.com/forums/t279256-returning-references.html)

Karlo Basic 11-15-2003 10:52 PM

Returning references
 
Hi!
I'm still just learning to program in C++ and I've got a question about
returning references.
Below is the code of my program. I think it's pretty obvious what it
does...
What doesn't work is shown in comments in the code:
************************************************** ****
#include <iostream>
using std::cin;
using std::cout;

class Koordinate
{
public:
Koordinate() : x(0),y(0){}
Koordinate(int a,int b) : x(a),y(b){}
friend Koordinate operator +(Koordinate p1, Koordinate p2){
Koordinate temp;
temp.x = p1.x + p2.x;
temp.y = p1.y + p2.y;
return temp;
}
/*
friend std::ostream& operator <<(std::ostream& output, Koordinate
point)
If I put the upper function heading instead of the one below then it
works normal.
But with the function header below the compiler complains(g++):
koordinate.cpp: In function 'int main()':
koordinate.cpp:XX:could not convert 'operator+(point2)' to 'Koordinate
&'
koordinate.cpp:XX: in passing argument 2 of 'operator<< (ostream
&,Koordinate &)'
*/
friend std::ostream& operator <<(std::ostream& output, Koordinate&
point){
output << "(" << point.x << "," << point.y << ")";
return output;
}
private:
int x,y;
};

int main()
{
Koordinate point(4,7), point2(2,3);
cout << (point + point2) << endl;
/*
If I put this instead:
Koordinate point(4,7), point2(2,3), temp;
temp = point + point2;
cout << temp << endl;
then it works without the need to change anything.
*/
return 0;
}
************************************************** *******
Thank you in advance for your time,
Karlo.



Rolf Magnus 11-16-2003 01:39 AM

Re: Returning references
 
Karlo Basic wrote:

> Hi!
> I'm still just learning to program in C++ and I've got a question
> about returning references.
> Below is the code of my program. I think it's pretty obvious what it
> does...
> What doesn't work is shown in comments in the code:
> ************************************************** ****
> #include <iostream>
> using std::cin;
> using std::cout;
>
> class Koordinate
> {
> public:
> Koordinate() : x(0),y(0){}
> Koordinate(int a,int b) : x(a),y(b){}
> friend Koordinate operator +(Koordinate p1, Koordinate p2){
> Koordinate temp;
> temp.x = p1.x + p2.x;
> temp.y = p1.y + p2.y;
> return temp;
> }
> /*
> friend std::ostream& operator <<(std::ostream& output, Koordinate
> point)
> If I put the upper function heading instead of the one below then it
> works normal.
> But with the function header below the compiler complains(g++):
> koordinate.cpp: In function 'int main()':
> koordinate.cpp:XX:could not convert 'operator+(point2)' to
> 'Koordinate
> &'
> koordinate.cpp:XX: in passing argument 2 of 'operator<< (ostream
> &,Koordinate &)'
> */
> friend std::ostream& operator <<(std::ostream& output, Koordinate&
> point){
> output << "(" << point.x << "," << point.y << ")";
> return output;
> }
> private:
> int x,y;
> };
>
> int main()
> {
> Koordinate point(4,7), point2(2,3);
> cout << (point + point2) << endl;


The result of (point + point2) is a nameless temporary Koordinate
objects, and it's not possible to bind a non-const reference to a
temporary in C++. Since your operator doesn't modify the object, that
parameter should be const anyway, so write your operator as

std::ostream& operator <<(std::ostream& output,
const Koordinate& point)

> /*
> If I put this instead:
> Koordinate point(4,7), point2(2,3), temp;
> temp = point + point2;
> cout << temp << endl;
> then it works without the need to change anything.


This time, temp is not a temporary, but a named variable, which can be
bound to a non-const reference.



All times are GMT. The time now is 10:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.