October 31st, 2005, 12:16 pm
QuoteOriginally posted by: samHi,Has anyone here used the complex C++ class before Header <complex>? I am trying to do something VERY straightforward but there doesnt seem to be any way to do it!Basically, I define a complex, and then want to re-asign its real and imaginary parts:complex<double> A(1,1); // assign real and im parts to 1.//Now I want to change the real and imaginary parts to 2, 2A.real() = 2; //error//The only way I can make it work is A = complex<double> (2,2);//but I am guessing that this is much less efficient because you are basically recalling the complex constructor followed by the assignment operator! Does anyone know how to do this? Thanks,From Josuttis "The C++ Standard Library" talking about the real() and imag() access functions: "Note that the return value is not a reference. Thus, you can't use these functions to modify the real or imaginary parts. To change only the real part or only the imaginary part you must assign a new complex number."So you'd have to create a new complex number with the value you wanted to set and the other value initialised from the current state.QuoteIt seems silly that you can not access the real and imaginary parts of the class via a function of the formdouble& complex<double>::real();I completely agree ...