469,963 Members | 1,344 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Reference parameters and anonymous objects

I have a bit of a problem with an overloaded operator that I'm writing that
I have distilled down to an example in the code below. Essentially, I have
an operator function (+=) that takes a reference parameter of class Poly.
If I use it with a named object (of type Poly), as in:

P1+=P4;

where P1 and P4 are both local variables of the class type, it works fine.
If I try to use an anonymous object on the right side, then I get an error
such as:

error: no match for `Poly& += Poly' operator

This wouldn't be much of a problem if another overloaded operator (+) didn't
return an anonymous copy of a Poly object, so if I write:

P1 += (P2 + P3);

then the RHS is an anonymous Poly object, and the += call won't compile.
This also happens with an overloaded << operator that takes a Poly & if I
try:

cout << (P2 + P3);

I can't find much written about function reference parameters and anonymous
objects. It makes some sense that forming a reference to an object that has
no other name might not be allowed, but I can't find anywhere that says
this. Note also, that this code compiles and works fine in MS Visual C++,
but won't compile on any version of g++ on Linux or the Mac that I've tried.
Any insights on this?

Thanks.

Mark Brandyberry
_________________________________________Poly.h
#include <iostream>
using namespace std;

class Poly {
private:
int i, j, k;
public:
Poly() {i=j=k=0;}
Poly(int x, int y, int z) {i=x;j=y;k=z;}
Poly operator+(Poly &);
void operator+=(Poly &);
friend ostream & operator<<(ostream &, Poly &);
};
________________________________________Poly.cpp
#include "Poly.h"

Poly Poly::operator+(Poly & p) {
Poly t(0,0,0);
t.i=p.i+i;
t.j=p.j+j;
t.k=p.k+k;
return(t);
}

void Poly::operator+=(Poly & p){
i+=p.i; j+=p.j; k+=p.k;
}

ostream & operator<<(ostream & strm, Poly & p) {
strm << "Poly i, j, k = " << p.i << "," << p.j << "," << p.k <<
endl;
return(strm);
}
________________________________________main.cpp
#include "Poly.h"

int main() {
Poly P1, P2(1,2,3), P3(4,5,6), P4;
cout << P1 << P2 << P3;
P1 = P2 + P3;
cout << P1;
P1=P4;
P1+=P2;
cout << P1;
P1=P4;
//the following line will not compile
P1+=(P2+P3);
//the following line will not compile
P1+=Poly(5,10,20);
cout << P1;
P1=P4;
//the following line will not compile
cout << (P2+P3);
return(0);
}

Jul 23 '05 #1
6 3247
"Mark Brandyberry" <md******@uiuc.edu> wrote in message
news:d1**********@news.ks.uiuc.edu
I have a bit of a problem with an overloaded operator that I'm
writing that I have distilled down to an example in the code below.
Essentially, I have an operator function (+=) that takes a reference
parameter of class Poly. If I use it with a named object (of type
Poly), as in:
P1+=P4;

where P1 and P4 are both local variables of the class type, it works
fine. If I try to use an anonymous object on the right side, then I
get an error such as:

error: no match for `Poly& += Poly' operator

This wouldn't be much of a problem if another overloaded operator (+)
didn't return an anonymous copy of a Poly object, so if I write:

P1 += (P2 + P3);

then the RHS is an anonymous Poly object, and the += call won't
compile. This also happens with an overloaded << operator that takes
a Poly & if I try:

cout << (P2 + P3);

I can't find much written about function reference parameters and
anonymous objects. It makes some sense that forming a reference to
an object that has no other name might not be allowed, but I can't
find anywhere that says this. Note also, that this code compiles and
works fine in MS Visual C++, but won't compile on any version of g++
on Linux or the Mac that I've tried. Any insights on this?

Thanks.

Mark Brandyberry
_________________________________________Poly.h
#include <iostream>
using namespace std;

class Poly {
private:
int i, j, k;
public:
Poly() {i=j=k=0;}
Poly(int x, int y, int z) {i=x;j=y;k=z;}
Poly operator+(Poly &);
void operator+=(Poly &);
friend ostream & operator<<(ostream &, Poly &);
};
________________________________________Poly.cpp
#include "Poly.h"

Poly Poly::operator+(Poly & p) {
Poly t(0,0,0);
t.i=p.i+i;
t.j=p.j+j;
t.k=p.k+k;
return(t);
}

void Poly::operator+=(Poly & p){
i+=p.i; j+=p.j; k+=p.k;
}

ostream & operator<<(ostream & strm, Poly & p) {
strm << "Poly i, j, k = " << p.i << "," << p.j << "," << p.k <<
endl;
return(strm);
}
________________________________________main.cpp
#include "Poly.h"

int main() {
Poly P1, P2(1,2,3), P3(4,5,6), P4;
cout << P1 << P2 << P3;
P1 = P2 + P3;
cout << P1;
P1=P4;
P1+=P2;
cout << P1;
P1=P4;
//the following line will not compile
P1+=(P2+P3);
//the following line will not compile
P1+=Poly(5,10,20);
cout << P1;
P1=P4;
//the following line will not compile
cout << (P2+P3);
return(0);
}

What you call "anonymous objects" are temporaries. You cannot supply
temporaries as arguments to functions/operators that take references to
objects as arguments. Your problems will be solved if you simply make the
functions/operators take references to *const* objects as arguments, e.g.,

void operator+=(const Poly &);

void Poly::operator+=(const Poly & p)
{
i+=p.i; j+=p.j; k+=p.k;
}
--
John Carson

Jul 23 '05 #2
The function signature for void Poly::operator+=(Poly & p) should be

Poly &Poly::operator+=(Poly & p);

Jul 23 '05 #3
Ignore my last post I am dreaming.

Jul 23 '05 #4

"John Carson" <jc****************@netspace.net.au> wrote in message
news:d1***********@otis.netspace.net.au...
"Mark Brandyberry" <md******@uiuc.edu> wrote in message
news:d1**********@news.ks.uiuc.edu
I have a bit of a problem with an overloaded operator that I'm
writing that I have distilled down to an example in the code below.
Essentially, I have an operator function (+=) that takes a reference
parameter of class Poly. If I use it with a named object (of type
Poly), as in:
P1+=P4;

<snip>

I can't find much written about function reference parameters and
anonymous objects. It makes some sense that forming a reference to
an object that has no other name might not be allowed, but I can't
find anywhere that says this. Note also, that this code compiles and
works fine in MS Visual C++, but won't compile on any version of g++
on Linux or the Mac that I've tried. Any insights on this?

Thanks.

Mark Brandyberry

<snip>
What you call "anonymous objects" are temporaries. You cannot supply
temporaries as arguments to functions/operators that take references to
objects as arguments. Your problems will be solved if you simply make the
functions/operators take references to *const* objects as arguments, e.g.,

void operator+=(const Poly &);

void Poly::operator+=(const Poly & p)
{
i+=p.i; j+=p.j; k+=p.k;
}
--
John Carson


John,

Yes, I knew that what some authors call anonymous objects are temporary,
which is why they're useful. Your response does fix the problem, and I
should have thought of it. I was trying to keep the example code as simple
as possible, so I didn't add the const qualifier. I think this is the first
place I've seen where the const qualifier is actually required, and not just
a good idea. I suppose it does make sense, since now the compiler can be
sure that you aren't going to try to change the reference, and thus change
the anonymous (temporary) object to no good purpose (since it won't be
available when you return from the function anyway). I assume VC++ is just
allowing incorrect code to go through? Or is this really part of the
defined C++ standard? I don't have any experience actually looking at the
standard...

Thanks!

Mark
Jul 23 '05 #5
"Mark Brandyberry" <md******@uiuc.edu> wrote in message
news:d1**********@news.ks.uiuc.edu

Yes, I knew that what some authors call anonymous objects are
temporary, which is why they're useful. Your response does fix the
problem, and I should have thought of it. I was trying to keep the
example code as simple as possible, so I didn't add the const
qualifier. I think this is the first place I've seen where the const
qualifier is actually required, and not just a good idea. I suppose
it does make sense, since now the compiler can be sure that you
aren't going to try to change the reference, and thus change the
anonymous (temporary) object to no good purpose (since it won't be
available when you return from the function anyway). I assume VC++
is just allowing incorrect code to go through? Or is this really
part of the defined C++ standard? I don't have any experience
actually looking at the standard...

Thanks!

Mark

The const is required by the C++ standard. VC++ doesn't require it,
presumably for some backwards compatibility reason.

--
John Carson

Jul 23 '05 #6
mark, take a look at "expression templates". CUJ had 1 or more articles
some time ago.

benedetto
Mark Brandyberry wrote:
"John Carson" <jc****************@netspace.net.au> wrote in message
news:d1***********@otis.netspace.net.au...
"Mark Brandyberry" <md******@uiuc.edu> wrote in message
news:d1**********@news.ks.uiuc.edu
I have a bit of a problem with an overloaded operator that I'm
writing that I have distilled down to an example in the code below. Essentially, I have an operator function (+=) that takes a reference parameter of class Poly. If I use it with a named object (of type
Poly), as in:
P1+=P4;

<snip>

I can't find much written about function reference parameters and
anonymous objects. It makes some sense that forming a reference to an object that has no other name might not be allowed, but I can't
find anywhere that says this. Note also, that this code compiles and works fine in MS Visual C++, but won't compile on any version of g++ on Linux or the Mac that I've tried. Any insights on this?

Thanks.

Mark Brandyberry
<snip>
What you call "anonymous objects" are temporaries. You cannot
supply temporaries as arguments to functions/operators that take references to objects as arguments. Your problems will be solved if you simply make the functions/operators take references to *const* objects as arguments, e.g.,
void operator+=(const Poly &);

void Poly::operator+=(const Poly & p)
{
i+=p.i; j+=p.j; k+=p.k;
}
--
John Carson


John,

Yes, I knew that what some authors call anonymous objects are

temporary, which is why they're useful. Your response does fix the problem, and I should have thought of it. I was trying to keep the example code as simple as possible, so I didn't add the const qualifier. I think this is the first place I've seen where the const qualifier is actually required, and not just a good idea. I suppose it does make sense, since now the compiler can be sure that you aren't going to try to change the reference, and thus change the anonymous (temporary) object to no good purpose (since it won't be available when you return from the function anyway). I assume VC++ is just allowing incorrect code to go through? Or is this really part of the defined C++ standard? I don't have any experience actually looking at the standard...

Thanks!

Mark


Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Howard | last post: by
1 post views Thread by Max Gay via .NET 247 | last post: by
3 posts views Thread by Adriano | last post: by
5 posts views Thread by Matt B | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.