Re: Overloading the "=" operator for Complex numbers
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 05/29/04
- Next message: EventHelix.com: "Re: Help with Visual C++.NET"
- Previous message: Pmb: "Re: Overloading the "=" operator for Complex numbers"
- In reply to: Pmb: "Re: Overloading the "=" operator for Complex numbers"
- Next in thread: Pmb: "Re: Overloading the "=" operator for Complex numbers"
- Reply: Pmb: "Re: Overloading the "=" operator for Complex numbers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: EventHelix.com: "Re: Help with Visual C++.NET"
- Previous message: Pmb: "Re: Overloading the "=" operator for Complex numbers"
- In reply to: Pmb: "Re: Overloading the "=" operator for Complex numbers"
- Next in thread: Pmb: "Re: Overloading the "=" operator for Complex numbers"
- Reply: Pmb: "Re: Overloading the "=" operator for Complex numbers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|