I'm trying to use the complex<> type to implement a Point class, for geometry problems.
I would like to be able to assign the value of the real and imaginary parts of the variable individually, through the = operand.
Like this:
class Point
{
public:
complex<int> my_point;
int& real(); // This should assign `my_point.real()` using `=`.
int& imag(); // Same for `my_point.imag()`.
}
Point p; // p is {0, 0}
p.real() = 10; // p is {10, 0}
p.imag() = 20; // p is {10, 20}
It would be very helpful being able to also retrieve these values using the same syntax.
cout << p.imag(); // p is {10, 20}, so this should print `20`.
How can I do that?
I guess this answers your question:
then use the way you wanted to use it to either assign or get the value of the real and imaginary points. maybe like this:
Just try it and let me know if it works or not
Edit Since the previous attempt didnt work, i guess you'd need to cast it. i tried using reinterpret cast and it worked for me. Heres the code to what i tried:
let me know about if it works or not for you, since it worked for me