Questions tagged [operator-overloading]

Operator overloading is a feature of a programming language that allows custom implementations for operators depending on the types of the operands involved. Some languages allow new operators to be defined while others only allow redefinition of existing ones.

Operator overloading is a feature of a programming language that allows custom implementations for operators depending on the types of the operands involved. Some languages allow new operators to be defined while others only allow redefinition of existing ones.

Popular questions

See also

7743 questions
140
votes
8 answers

Should operator<< be implemented as a friend or as a member function?

That's basically the question, is there a "right" way to implement operator<< ? Reading this I can see that something like: friend bool operator<<(obj const& lhs, obj const& rhs); is preferred to something like ostream& operator<<(obj const&…
Federico Builes
  • 4,549
  • 3
  • 30
  • 47
138
votes
5 answers

Overloading member access operators ->, .*

I understand most operator overloading, with the exception of the member access operators ->, .*, ->* etc. In particular, what is passed to these operator functions, and what should be returned? How does the operator function (e.g. operator->(...) )…
Bingo
  • 3,515
  • 5
  • 21
  • 24
138
votes
9 answers

Is there actually a reason why overloaded && and || don't short circuit?

The short circuiting behaviour of the operators && and || is an amazing tool for programmers. But why do they lose this behaviour when overloaded? I understand that operators are merely syntactic sugar for functions but the operators for bool have…
129
votes
2 answers

Operator overloading : member function vs. non-member function?

I read that an overloaded operator declared as member function is asymmetric because it can have only one parameter and the other parameter passed automatically is the this pointer. So no standard exists to compare them. On the other hand,…
126
votes
6 answers

Is there a way to do method overloading in TypeScript?

Is there a way to do method overloading in TypeScript language? I want to achieve something like this: class TestClass { someMethod(stringParameter: string): void { alert("Variant #1: stringParameter = " + stringParameter); } …
Void-995
  • 1,555
  • 2
  • 12
  • 8
120
votes
10 answers

C# operator overload for `+=`?

I am trying to do operator overloads for +=, but I can't. I can only make an operator overload for +. How come? Edit The reason this is not working is that I have a Vector class (with an X and Y field). Consider the following example. vector1 +=…
Mathias Lykkegaard Lorenzen
  • 13,158
  • 19
  • 85
  • 163
119
votes
13 answers

How do I check for nulls in an '==' operator overload without infinite recursion?

The following will cause infinite recursion on the == operator overload method Foo foo1 = null; Foo foo2 = new Foo(); Assert.IsFalse(foo1 == foo2); public static bool operator ==(Foo foo1, Foo foo2) { if (foo1 == null)…
Andrew Jones
  • 4,637
  • 5
  • 23
  • 19
112
votes
5 answers

How to overload the operator++ in two different ways for postfix a++ and prefix ++a?

How to overload the operator++ in two different ways for postfix a++ and prefix ++a?
rookie
  • 7,085
  • 13
  • 42
  • 57
106
votes
5 answers

__lt__ instead of __cmp__

Python 2.x has two ways to overload comparison operators, __cmp__ or the "rich comparison operators" such as __lt__. The rich comparison overloads are said to be preferred, but why is this so? Rich comparison operators are simpler to implement…
Roger Pate
103
votes
9 answers

Javascript: operator overloading

I've been working with JavaScript for a few days now and have got to a point where I want to overload operators for my defined objects. After a stint on google searching for this it seems you can't officially do this, yet there are a few people out…
Lee Brindley
  • 6,081
  • 3
  • 30
  • 58
97
votes
5 answers

operator << must take exactly one argument

a.h #include "logic.h" ... class A { friend ostream& operator<<(ostream&, A&); ... }; logic.cpp #include "a.h" ... ostream& logic::operator<<(ostream& os, A& a) { ... } ... When i compile, it says: std::ostream& logic::operator<<(std::ostream&,…
As As
  • 1,919
  • 3
  • 16
  • 29
96
votes
17 answers

Operator[][] overload

Is it possible to overload [] operator twice? To allow, something like this: function[3][3](like in a two dimensional array). If it is possible, I would like to see some example code.
icepopo
  • 1,527
  • 3
  • 15
  • 12
91
votes
9 answers

How would you overload the [] operator in javascript

I can't seem to find the way to overload the [] operator in javascript. Anyone out there know? I was thinking on the lines of ... MyClass.operator.lookup(index) { return myArray[index]; } or am I not looking at the right things.
slyprid
  • 1,029
  • 1
  • 9
  • 9
87
votes
16 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

One of my pet hates of C-derived languages (as a mathematician) is that (-1) % 8 // comes out as -1, and not 7 fmodf(-1,8) // fails similarly What's the best solution? C++ allows the possibility of templates and operator overloading, but both…
P i
  • 25,182
  • 33
  • 133
  • 229
79
votes
11 answers

When to Overload the Comma Operator?

I see questions on SO every so often about overloading the comma operator in C++ (mainly unrelated to the overloading itself, but things like the notion of sequence points), and it makes me wonder: When should you overload the comma? What are some…
user541686
  • 189,354
  • 112
  • 476
  • 821