views:

42

answers:

3

I know that we can't overload operator with other meaning, we can't create new operators, and we can't overload without user-defined class. If I overload operators incorrectly? what errors will report? compiler errors or runtime error?

If I overload **, what would happen?

+2  A: 

You can overload only existing operators. There is no operator ** in C++.

If you try, the compiler would complain.

Operator overloads are checked at the compile time. If it compiles, it's just a kind of function, so the possible runtime errors are the same as for any other function.

Vlad
+1  A: 

Perhaps I'm misunderstanding your question, but you can definitely overload operators with different meanings. Consider integers vs iostreams:

1 << 5;  // takes the value 1 and does a binary shift

cout << "moo";  // inserts the string "moo" into the cout stream

Anyway, operator overloads are just functions. Depending on what you do, you may get a compile error or a runtime error. It depends on the specific error.

R Samuel Klatchko
A: 

Also, keep in mind that if you are manipulating objects on the heap (via the new operator), incorrectly overloading operators can cause crashes or memory leaks, as well as just not doing what you intended. These types of problems may not generate runtime or compile time errors.

David Lazell