From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,3bb0dcf4dc92e48d,start X-Google-Attributes: gid109fba,public X-Google-Thread: fee84,3bb0dcf4dc92e48d,start X-Google-Attributes: gidfee84,public From: mdeters@cs.bgsu.edu (Morgan Deters) Subject: Re: Mysterious DEC OSF C++ compiler warning Date: 1995/12/18 Message-ID: #1/1 X-Deja-AN: 122198853 references: <4b4rog$m80@holly.cc.uleth.ca> organization: Bowling Green State University, Bowling Green, Ohio newsgroups: comp.lang.c++,comp.unix.osf.osf1 In article <4b4rog$m80@holly.cc.uleth.ca>, Marc Roussel wrote: > Here's a simple program for which the DEC OSF C++ compiler generates a (to me) > mysterious warning: > > // foo.h: > class junk > { double a; > public: > junk(void) {}; > junk(double x) { a = x;}; > junk & operator = (junk & x) { a = x.a; return(*this);}; > }; > > // foo.c > #include > #include > #include "foo.h" > > > main() > { > junk x; > > x = junk(2); > } > > The warning is > > foo.c:10: warning: In this statement, the initialization of a non-const > reference requires a temporary for "junk(2)". junk::operator=(junk&) takes a non-const reference as a parameter. You are supplying a temporary, junk(2), to the operator=() method. If operator=() is not to modify its argument (which it really shouldn't do, if you model your overloaded operators after built-in operator concepts), declare operator=() as: junk& operator=(const junk& x) { a = x.a; return *this; } This will clear the problem. The compiler doesn't like it because the temporary junk(2) could be altered, which is ridiculous because you no longer have access to it. ____________________________________________________________________ Morgan Deters mdeters@cs.bgsu.edu Bowling Green State University Bowling Green, Ohio