Reference parameters and anonymous objects

M

Mark Brandyberry

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::eek:perator+(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::eek:perator+=(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);
}
 
J

John Carson

Mark Brandyberry said:
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::eek:perator+(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::eek:perator+=(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::eek:perator+=(const Poly & p)
{
i+=p.i; j+=p.j; k+=p.k;
}
 
F

falcon

The function signature for void Poly::eek:perator+=(Poly & p) should be

Poly &Poly::eek:perator+=(Poly & p);
 
M

Mark Brandyberry

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::eek:perator+=(const Poly & p)
{
i+=p.i; j+=p.j; k+=p.k;
}

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
 
J

John Carson

Mark Brandyberry said:
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.
 
B

betto73

mark, take a look at "expression templates". CUJ had 1 or more articles
some time ago.

benedetto
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top