OK that's interesting. The semantic trickery I missed is the effect of declaring
Originally Posted by brewbuck
If I then initialize that with
double &real ;
double &imag ;
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,
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 ;
This allows something like
complex_ref &operator=(const complex_ref &complex_p)
real = complex_p.real;
imag = complex_p.imag;
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.
complex_ref cr(c1.rval + 25, c1.ival + 25) ;
cr = std::complex<double>(1.0,-1.0) ;
By what you are saying there is a vast difference between assignment and initialization for reference values. ie
Is this correct?
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?
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.