Re: Overloading the "=" operator for Complex numbers

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 05/29/04


Date: Sat, 29 May 2004 12:08:11 +0100


> >
> > Forgetting const, put the const in and it will compile.
> >
> > Complex& operator=(const Complex & );
> >
> > What you are failing to realise is that you cannot bind a temporary to a
> > non-const reference.
>
> I see no reason for that to be true.

You might not see a reason, other people don't see the reason, its somewhat
controversial, but its in the C++ standard in black and white. However some
compilers do not enforce that rule.

> >
> > sum = u + v;
> >
> > u + v returns a temporary, you have declared your operator= with a
> non-const
> > reference. Therefore you cannot use u + v on the right hand side of a
> > operator=.
> >
> > Just add const.
>
> I disagree. I simplified the code I'm working on to the bare bones. It
now
> reads

Fine but you've changed the program to avoid the problem I described. You no
longer have a temporary on the rhs of an assignment.

>
> _______________________________________
> #include <iostream.h>
>
> class Complex {
> public:
> Complex( float = 0.0, float = 0.0 );
> Complex &operator=( Complex & );
> float getRe() { return re; };
> float getIm() { return im; };
> private:
> float re;
> float im;
> };
>
> Complex::Complex( float a, float b )
> : re( a ), im( b) { }
>
> Complex& Complex::operator=( Complex &z )
> {
> re = z.re;
> im = z.im;
>
> return *this;
> }
>
>
> int main()
> {
> Complex z1( 1, 5 ), z2( 0, 0 );
> float x, y;
>
> x = z2.getRe();
> y = z2.getIm();
>
> cout << "Components before 'operator=' call: (x,y) = ("
> << x << "," << y << ")" << endl;
>
> z2 = z1;
>
> x = z2.getRe();
> y = z2.getIm();
>
> cout << "\nComponents after 'operator=' call: (x,y) = ("
> << x << "," << y << ")" << endl;
>
> return 0;
> }
> _______________________________________
>
> This program works just fine. The output is
>
> ------------
> Components before 'operator=' call: (x,y) = (0,0)
>
> Components after 'operator=' call: (x,y) = (1,5)
> ------------
>
> That is exactly what I want it to do. Had I wanted to use this code
further
> then perhaps I'd used const's. Not while I'm in a learning mode. When
> learning one seeks to understand when something is required and when it is
> not required and how it affects the logic etc. Perhaps everyone disagrees
> with the way I chose to learn. But that is the way I choose to learn

I was just answering the question you asked, 'What have I done wrong this
time?' I pointed out a way (the only way) to make your program compile, you
chose to write a different program instead.

john



Relevant Pages

  • Re: Overloading the "=" operator for Complex numbers
    ... > I'm not interested in worrying about const and program integrity at this ... > float magnitude(); ... Say rather, it does not _compile_. ...
    (comp.lang.cpp)
  • Re: problem with cast and unions
    ... Remember, just getting it to compile won't necessarily get it to work, even if you have done something that seems sensible to solve each problem. ... You are receiving an int as a parameter, converting it to a float and then returning the bit pattern of that float interpretted as an int. ...
    (comp.lang.c)
  • Re: Changes in 2005.
    ... VS.NET 2003 does not compile to unmanaged code. ... There is all the more reason for startups to write Web-based software now, ... are going to tell me that you would rather write an ASP.NET app ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Include Statement
    ... For the same reason conditional compilation directives were added to compile a block vs not to compile a block. ... For the same reason you have IMPORTS to reference an compiled object. ... Second, it is difficult to explain all the reason because they are VAST, every programmer has their reasons. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Can one compile khello.cc ?
    ... >upgrade KDE on it. ... >QCString const&, bool, bool)' ... : undefined reference to `QObject::connect(QObject const*, char const*, ... compile to an object file only however, let's test using -c for 'compile ...
    (freebsd-questions)