Using Complex C++ Class Header <complex>

  • Context: C/C++ 
  • Thread starter Thread starter sam2
  • Start date Start date
  • Tags Tags
    C++ Class Complex
Click For Summary

Discussion Overview

The discussion revolves around the use of the complex C++ class defined in the header . Participants are exploring how to reassign the real and imaginary parts of a complex number after its initial definition, addressing both practical implementation and theoretical implications.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses frustration over the inability to directly modify the real and imaginary parts of a complex number using the methods provided, suggesting that it seems inefficient to reassign the entire complex number instead.
  • Another participant cites Microsoft's documentation, stating that the template function cannot be used to modify the real part, and emphasizes that a new complex number must be created for such changes.
  • A different viewpoint suggests that the methods for accessing real and imaginary parts should be private, advocating for the use of set and get methods or operator overloading for arithmetic operations.
  • One participant introduces the concept of immutable classes, noting their potential benefits for optimization and suggesting that this design choice may be intentional.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the design of the complex class or the implications of its immutability. There are competing views on whether the current implementation is efficient or appropriate.

Contextual Notes

Limitations include the lack of clarity on the specific implementation details of the complex class across different compilers, as well as the potential performance implications of creating new complex instances versus modifying existing ones.

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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
6
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
81
Views
8K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 6 ·
Replies
6
Views
13K