shangping shangping - 1 month ago 16
C++ Question

Can I create a new operator in C++ and how?

MATLAB arrays support matrix operations and element operations. For example,

M*N
and
M.*N
. This is a quite intuitive way to distinguish ‎the two different operations. If I want to implement similar operations in C++, how can I do that?

Can I create a new operator,
.*
, too? If yes, can anyone give me some guidance?

Answer

Here's a nice tutorial on operator overloading. Here's an example on how to create custom operators.

Note however that custom operators are not supported by C++ per se, the example above uses tricks with macros to achieve the effect. I wouldn't do it if I were you, and use functions instead. Without tricks - custom operators are not supported in C++.

Comments