Originally Posted by
brewbuck
You have to invent some kind of reference object which encapsulates the action of setting the separate real and imaginary parts.
OK that's interesting. The semantic trickery I missed is the effect of declaring
Code:
double &real ;
double &imag ;
If I then initialize that with
Code:
carr c1(100) ;
real = *(c1.rval + k) ;
imag = *(c1.rval + k) ;
// this assigns to the k'th element of the carr array?
real = 1.0 ;
imag = -1.0 ;
What you are saying is that you then wrap real and imag in a class and overload the assignment operator with the code segment you provided,
Code:
complex_ref &operator=(const complex_ref &complex_p)
{
real = complex_p.real;
imag = complex_p.imag;
}
This allows something like
Code:
complex_ref cr(c1.rval + 25, c1.ival + 25) ;
cr = std::complex<double>(1.0,-1.0) ;
That's pretty nifty, and a subtle point with regards to the meaning of a 'reference' value, which is a kind of pointer in disguise I suppose. I'm actually a little confused by this however, so I'm not sure if I really understand this.
By what you are saying there is a vast difference between assignment and initialization for reference values. ie
Code:
double x = 5.0 ;
double y = 6.0 ;
double &r(x) ;
// r == 5
r = 7 ; // x = 7
r = y ; // x = y = 6.0
// how do I change r's reference to point to y?
Is this correct?
Edit:
It seems to be correct based on a quick search. However reference can only be initialized once. You can't change what they are pointing to.