How to assign the real and imaginary parts of complex variables, individually, using the = operator in c++?

83 Views Asked by At

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?

2

There are 2 best solutions below

19
Shreeyash Shrestha On BEST ANSWER

I guess this answers your question:

class Point
{
public:
    std::complex<int> my_point;

    int& real()
    {
        return my_point.real();
    }

    int& imag()
    {
        return my_point.imag();
    }

    int real() const
    {
        return my_point.real();
    }

    int imag() const
    {
        return my_point.imag();
    }
};

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:

int main()
{
    Point p;
    p.real() = 10;
    p.imag() = 20;

    std::cout << p.imag() << std::endl;

    return 0;
}

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:

//same code as previous
    int& real()
    {
        return reinterpret_cast<int*>(&my_point)[0];
    }

    int& imag()
    {
        return reinterpret_cast<int*>(&my_point)[1];
    }

    int real() const
    {
        return reinterpret_cast<const int*>(&my_point)[0];
    }

    int imag() const
    {
        return reinterpret_cast<const int*>(&my_point)[1];
    }
//remaining same code here as well 

let me know about if it works or not for you, since it worked for me

0
doug On

Using The exception that allows constexpr as shown here and adjusting Shreeyash's ideas:

For any object z of type std::complex, reinterpret_cast<T(&)[2]>(z)[0] is the real part of z and reinterpret_cast<T(&)[2]>(z)[1] is the imaginary part of z

Just replace T's with int and drop the template if all you need is an int complex type.

#include <complex>
#include <iostream>

template <class T>  // floating point type
class Point
{
public:
    std::complex<T> my_point;

    T& real()
    {
        return reinterpret_cast<T(&)[2]>(my_point)[0];
    }

    T& imag()
    {
        return reinterpret_cast<T(&)[2]>(my_point)[1];
    }

    T real() const
    {
        return my_point.real();
    }

    T imag() const
    {
        return my_point.imag();
    }
};

int main()
{
    Point<int> p;
    p.real() = 10;
    p.imag() = 20;

    std::cout << p.imag() << std::endl;
    return 0;
}