Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: mdeters@cs.bgsu.edu (Morgan Deters)
Subject: Re: Mysterious DEC OSF C++ compiler warning
Date: 1995/12/18	[thread overview]
Message-ID: <mdeters-1812951946010001@ppp-k-29.bgsu.edu> (raw)
In-Reply-To: 4b4rog$m80@holly.cc.uleth.ca

In article <4b4rog$m80@holly.cc.uleth.ca>, Marc Roussel
<roussel@henri.chem.uleth.ca> 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 <iostream.h>
> #include <math.h>
> #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



  reply	other threads:[~1995-12-18  5:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-12-18  5:00 Mysterious DEC OSF C++ compiler warning Marc Roussel
1995-12-18  5:00 ` Morgan Deters [this message]
1995-12-19  5:00 ` Andrew Koenig
1995-12-20  5:00 ` Tim Lucia
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox