我可以创建一个新的操作符在C + +如何? [英] can I create a new operator in C++ and how?
问题描述
Matlab数组支持矩阵运算和元素运算。例如 M * N
和 M. * N
。这是一个非常直观的方式来区分两个不同的操作。如果我想在C ++中实现类似的操作,我该怎么做?我可以创建一个新的运算符*吗?如果是,任何人都可以给我一些指导?
Matlab arrays support matrix operation and element operation. For example M*N
and M.*N
. This is a quite intuitive way to distinct 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?
推荐答案
这里有一个很好的教程操作符重载。 以下是关于如何创建自定义运算符的示例。
但是请注意,自定义运算符本身不支持C ++,上面的示例使用宏来实现效果的技巧。如果我是你,我不会这样做,而使用函数。没有小技巧 - 在C ++中不支持自定义运算符。
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++.
这篇关于我可以创建一个新的操作符在C + +如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!