473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 3429
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
The following class method is being rejected by the PHP parser. If I change the method paramaters and allow the objects to be passed as copies, then the parser has no problem. Or, if I pass by...
3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
7
by: Howard | last post by:
Hi, in one of the recent posts, I saw someone pass two variables of built-in type (int and double), by reference, to a member function of a class. That function then took the addresses of those...
2
by: Steve Jorgensen | last post by:
When writing VB or VBA code that works with databases or other external libraries that cannot be trusted to automatically do the right thing when references to their objects are arbitrarily...
2
by: Mountain Bikn' Guy | last post by:
It is known that one cannot pass arguments as ref or out in a marshal-by-reference class. My problem is that I have a C DLL (and C# wrapper) that I need to isolate in an AppDomain and then I need...
1
by: Max Gay via .NET 247 | last post by:
I am having a problem with a page that I am trying to develop. Iam new at asp.net. The page I have created makes a datagrid that has two links aedit and a cancel link. When you click either of the...
3
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
5
by: Matt B | last post by:
Hi all, I am creating a web service which takes in a bulk of training text for each of two authors, along with a third bulk of anonymous text, and predicts which author is most likely to have...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.