1

I would like to overload the || operator in such a way that I can use it to find the modulus of a complex number. I.E.

    Complex z(x0,y0);
    double mod = |z|;

Is it possible to do so? If so, how?

Thanks

2
  • Can you use the functions of the complex type, instead of creating new operators? Nov 6, 2015 at 21:27
  • Is there any reason against creating a named function for modulo instead of using an operator? If your function returns a reference, you could chain the calculation. Nov 6, 2015 at 21:28

1 Answer 1

7

No, that's not possible. There is no |x| operator (there is no such syntax). || is logical OR.

Generally speaking, it's a good idea to avoid writing code that attempts to change the syntax of C++ because it makes the code much less maintainable. Imagine that you figured out a way to create a |x| operator (I don't think there's a way...). The next person that read your code who knew C++ would have no idea what that was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.