Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unoverloadable C++ operators

What operators can not be overloaded in C++?

like image 885
MKaufmann Avatar asked May 20 '10 14:05

MKaufmann


People also ask

Can we overload [] operator?

where () is the function call operator and [] is the subscript operator. You cannot overload the following operators: .

Can you override operators in C?

C does not support overloading of operators or functions. There's no way you can redefine < , <= , > , >= , == , or != to compare struct types directly.

Can we overload * operator in C++?

Operator Overloading in C++ In C++, we can make operators work for user-defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.

How do you overload an operator?

Operator Overloading in Binary Operators Here, + is a binary operator that works on the operands num and 9 . When we overload the binary operator for user-defined types by using the code: obj3 = obj1 + obj2; The operator function is called using the obj1 object and obj2 is passed as an argument to the function.


2 Answers

I'm pretty sure the C++ FAQ Lite probably covers this. The ones I can think of right off are the ternary operator, the . operator and the scope resolution operator (::). Thinking a moment, since the . operator can't be overloaded, .* probably can't be either.

There are also some operators that can but almost never should be overloaded, including the comma operator, &&, ||, all of which normally create a sequence point. The && and || also only (normally) evaluate the right operand if necessary. Neither of those characteristics is true of an overloaded operator.

While there are a few reasons to do so, overloading the unary & (address-of) operator is also often a pretty poor idea. The address of an object is largely equated with its identity, so overloading it can make quite a few other things relatively difficult.

Edit: as far as evaluating the right operand only if necessary (aka "Short circuit evaluation"): consider something like x && y. The expression can only be true if if the left operand is true. If the left operand evaluates to false, then the expression must also be false, and C (and C++) guarantee that the right operand will not be evaluated at all. This is convenient (for example) if you want to do something like if (ptr != NULL && ptr->member /*...*/ ). In this case, if the pointer in NULL, execution stops, and you never attempt to dereference the pointer.

The same basic idea is true with ||, but in reverse. In this case, if the left operand evaluates to true, then the expression as a whole must be true, regardless of what the right operand would evaluate to so (again) C and C++ guarantee that in this case the right operand won't be evaluated.

When you overload those operators, however, evaluating the expression all will always evaluate both operands. The first expression would attempt to dereference the pointer even if it is a null pointer, so it would give undefined behavior.

like image 85
Jerry Coffin Avatar answered Oct 17 '22 18:10

Jerry Coffin


From Wikipedia:

Operator Name                           Syntax
Bind pointer to member by reference     a.*b
Member                                  a.b
Scope resolution                        a::b
Size of                                 sizeof(a)
Ternary                                 a ? b : c
Type identification                     typeid(a)
like image 9
Jaap Weidemann Avatar answered Oct 17 '22 18:10

Jaap Weidemann