C/C++ Using Complex C++ Class Header <complex>

AI Thread Summary
The discussion centers around the limitations of the C++ complex class from the <complex> header, specifically regarding the inability to directly modify the real and imaginary parts of a complex number after its creation. Users express frustration that while one can create a complex number using `complex<double> A(1,1);`, changing its components requires creating a new instance, as demonstrated by `A = complex<double>(2,2);`. This method is perceived as less efficient due to the overhead of invoking the constructor again. Participants suggest that the design of the class could be improved by implementing getter and setter methods for the real and imaginary parts, rather than allowing direct access, which is currently not possible. They reference Microsoft's documentation indicating that the complex class is immutable, meaning its components cannot be changed after instantiation, which can lead to optimization benefits in certain scenarios. The conversation highlights a need for clearer design in the class to enhance usability while maintaining efficiency.
sam2
Messages
22
Reaction score
0
Hi,

Has anyone here used the complex C++ class before Header <complex>? I am trying to do something VERY straightforward but there doesn't 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 I am parts to 1.

//Now I want to change the real and imaginary parts to 2, 2

A.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 form

double& complex<double>::real();

Thanks,
 
Technology news on Phys.org
sam2 said:
Hi,
Has anyone here used the complex C++ class before Header <complex>? I am trying to do something VERY straightforward but there doesn't 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 I am parts to 1.
//Now I want to change the real and imaginary parts to 2, 2
A.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 form
double& complex<double>::real();
Thanks,

A.real() and its counterpart for the imaginary portion should be private rather than public attributes. There should be a set and get method, or "+", "-", "/" and "*" should be overridden so you can add another complex number to it.
 
To quote Microsoft's documentation:

This template function cannot be used to modify the real part of the complex number. To change the real part, a new complex number must be assigned the component value.


Now, to allay your fears about efficiency, if the implementation and compiler are any good, the expression:

a = complex<int>(5, imag(a));

should be just as efficient as if you could write

real(a) = 5;

.
 
It seems silly that you can not access the real and imaginary parts of the class via a function of the form

Such a class is called an immutable class. They can be useful in certain circumstances and may help the compiler with optimisation.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top