简体   繁体   中英

C++ creating new operators and overloading the current ones

I am writing code for transposing a matrix as a part of learning C++. What I am doing is operator overloading. But, usually the notation for transposing a matrix is ' .

Is ' even an operator? I understand if it is between ' ' you notate a character, and if you use " " you notate a string. But are these operators or not? Can I overload them as individual characters or even as whole?

Can I make new operators in C++ that only work on an object of a specific class? The trick I've used is overloading the known operators like + , - , ^ , ... and then with #define I change how the preprocessor processes the text, but in the end I would still override an existing operator and I cannot use any of the special symbols but rather letters.

Operator overloading is limited to the existing set of C++ operators. It's operator overloading , not creation . You cannot make new operators like @ , nor can you turn non-operator characters like ' into operators.

Can I make new operators in C++ that only work on an object of a specific class?

You can't make new operators. You can only repurpose existing ones.

...then with #define I change how the preprocessor processes the text...

You probably shouldn't be doing that. Stay away from the preprocessor. It's a tool of last resort.

You cannot create operators that don't exist as part of the C++ standard. They would not be part of the grammar and would lead to syntax errors.

So operator possibilities are linked to what the language syntax and grammar authorize (see @afenster answer for a link to the list).

On a side note, this operator issue is also a true in other languages like Python, where the committee needs to decide whether adding a new operator (like @ for matrix multiplication) makes sense for the language or not. Lots of operators are derived from mathematical expressions, so adding new operators that don't have this mathematical background would not make sense (exceptions are * , () , for instance)

The list of operators you can overload is here . Unfortunately (or fortunately), you cannot use any other character as an operator. The most basic reason for that is that operator overloading does not and must not change the logic of the syntax parsing of your code (such as what is an operator and what is not, what is the operator precedence, etc.).

You can only overload existing operators.

You can fake it a bit.

namespace my_operator {
  struct transpose_t {};
  constexpr tranpose_t transpose{};

  template<class Matrix>
  Matrix operator^( Matrix lhs, tranpose_t ) {
    lhs.Transpose();
    return lhs;
  }
}

given that, and a matrix type that has a .Transpose() method that self-transposes, we get:

using my_operator::transpose;

MATRIX m = {/* whatever */};
MATRIX tm = m^transpose;

and tm will be the transpose of m .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM