Page 1 of 1

complex C++ class

Posted: October 31st, 2005, 11:47 am
by sam
Hi,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? It seems silly that you can not access the real and imaginary parts of the class via a function of the formdouble& complex<double>::real();Thanks,

complex C++ class

Posted: October 31st, 2005, 12:11 pm
by ldrage
QuoteBasically, 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; //errorThe correct syntax is A.real(2);lee

complex C++ class

Posted: October 31st, 2005, 12:14 pm
by sam
I knew I was missing something!Many Thanks Lee.Regards,

complex C++ class

Posted: October 31st, 2005, 12:16 pm
by MattF
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 ...

complex C++ class

Posted: October 31st, 2005, 1:30 pm
by ldrage
I'm not a C++ expert so maybe I'm missing something but I looked at the overloads for real(..) in complex<_Ty> and there are two (at least in VC++): _Ty real() _Ty real(const _Ty &right)The attached code produced the output:<1,1><2,2>and so it looks like I'm using member functions correctly. Let me know if you disagree - I'm always keen to learn.

complex C++ class

Posted: October 31st, 2005, 2:06 pm
by ckarakus
QuoteOriginally posted by: ldrageI'm not a C++ expert so maybe I'm missing something but I looked at the overloads for real(..) in complex<_Ty> and there are two (at least in VC++): _Ty real() _Ty real(const _Ty &right)As you see, return is by value. You cannot assign to primitive values returned by a function.If it was _Ty& real()then you can write s.t likex.real() = 3

complex C++ class

Posted: October 31st, 2005, 2:57 pm
by ldrage
That's understood but my point was that you can instead use x.real(2.0) in the absence of Ty& real() and so there is no need to assign a new complex number.

complex C++ class

Posted: October 31st, 2005, 3:03 pm
by sam
A.real(2) does work and it sets the real part to 2. I glanced over the header file where the class and its members/methods are declared and there isnt an overloaded version of the A.real() function which returns by reference, hence what I was initially trying to do was failing...Thanks,

complex C++ class

Posted: October 31st, 2005, 3:21 pm
by Alan
A=2.0+2.0*I