473,669 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<(ostr eam &, 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<<(ostr eam & 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,2 0);
cout << P1;
P1=P4;
//the following line will not compile
cout << (P2+P3);
return(0);
}

Jul 23 '05 #1
6 3444
"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<<(ostr eam &, 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<<(ostr eam & 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,2 0);
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+=(cons t 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.n et.au> wrote in message
news:d1******** ***@otis.netspa ce.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+=(cons t 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.n et.au> wrote in message
news:d1******** ***@otis.netspa ce.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+=(cons t 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
2844
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 reference but set no default value, then the parser has no problems. I've resovled this for now by taking out the "false" default values. But how shall I handle those situations (there are many) where I wish to pass an object by reference but I'm not...
3
1853
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 include files: typedef Context *Context_ptr; typedef ObjOut<Context> Context_out;
7
9541
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 reference parameters and stored the results in member pointer variables. Is that guaranteed to be ok? In other words, does taking the address of a reference to a built-in type give you the address of the original variable? I'd expect so (not...
2
3010
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 released, some thought must be put into how to make sure the objects will all be closed and released in the correct order, even in the result of an error. This requirement can make our code really ugly, even following the best of commonly known best...
2
4710
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 to interact with many objects in that DLL and wrapper by reference. (So the classes inherit from MBRO.) While my app is running, I need to obtain info from objects in the 2nd app domain frequently/speedily and I need update the GUI; and I need to...
1
240
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 links itthrows the following error "Object reference noet set to aninstance of an object. The following lines appear along withthat error message Line 69: Dim CurrentTextBox AsSystem.Web.UI.WebControls.Textbox Line 70: ...
3
2434
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 object :(((( Anyone pls help me to solve this problem!!! thanks in advance,
5
1454
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 written the anonymous text. I have no experience with web services and I am confused about the best way to design this - particularly with regards to the parameters that the service is passed.
275
12250
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
8895
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8809
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2797
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.