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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top