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
2255
votes
7 answers

What are the basic rules and idioms for operator overloading?

Note: The answers were given in a specific order, but since many users sort answers according to votes, rather than the time they were given, here's an index of the answers in the order in which they make the most sense: The General Syntax of…
sbi
  • 204,536
  • 44
  • 236
  • 426
437
votes
17 answers

Why doesn't Java offer operator overloading?

Coming from C++ to Java, the obvious unanswered question is why didn't Java include operator overloading? Isn't Complex a, b, c; a = b + c; much simpler than Complex a, b, c; a = b.add(c);? Is there a known reason for this, valid arguments for not…
rengolin
397
votes
0 answers

Pretty-print C++ STL containers

I would like to have a single template that once and for all takes care of pretty-printing all STL containers via operator<<. In pseudo code, I'm looking for something like this: template
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
362
votes
10 answers

How to overload __init__ method based on argument type?

Let's say I have a class that has a member called data which is a list. I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with an actual list. What's your technique for doing…
Baltimark
  • 8,306
  • 12
  • 34
  • 35
309
votes
4 answers

How do I overload the [] operator in C#

I would like to add an operator to a class. I currently have a GetValue() method that I would like to replace with an [] operator. class A { private List values = new List(); public int GetValue(int index) => values[index]; }
Adam Tegen
  • 23,348
  • 32
  • 115
  • 149
294
votes
15 answers

Are == and != mutually dependent?

I'm learning about operator overloading in C++, and I see that == and != are simply some special functions which can be customized for user-defined types. My concern is, though, why are there two separate definitions needed? I thought that if a == b…
BarbaraKwarc
  • 2,511
  • 2
  • 10
  • 17
290
votes
1 answer

A positive lambda: '+[]{}' - What sorcery is this?

In Stack Overflow question Redefining lambdas not allowed in C++11, why?, a small program was given that does not compile: int main() { auto test = []{}; test = []{}; } The question was answered and all seemed fine. Then came Johannes…
Daniel Frey
  • 52,317
  • 11
  • 110
  • 168
263
votes
8 answers

How do I overload the square-bracket operator in C#?

DataGridView, for example, lets you do this: DataGridView dgv = ...; DataGridViewCell cell = dgv[1,5]; but for the life of me I can't find the documentation on the index/square-bracket operator. What do they call it? Where is it implemented? Can…
Coderer
  • 21,290
  • 22
  • 71
  • 124
260
votes
3 answers

How to override the [] operator in Python?

What is the name of the method to override the [] operator (subscript notation) for a class in Python?
Sahas
  • 8,807
  • 8
  • 37
  • 49
253
votes
6 answers

How to properly overload the << operator for an ostream?

I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian…
226
votes
3 answers

Override Python's 'in' operator?

If I am creating my own class in Python, what function should I define so as to allow the use of the 'in' operator, e.g. class MyClass(object): ... m = MyClass() if 54 in m: ...
astrofrog
  • 26,293
  • 28
  • 81
  • 123
190
votes
10 answers

Operator overloading in Java

Please can you tell me if it is possible to overload operators in Java? If it is used anywhere in Java could you please tell me about it.
Vipul
  • 2,429
  • 5
  • 22
  • 28
179
votes
6 answers

Operator Overloading with C# Extension Methods

I'm attempting to use extension methods to add an operater overload to the C# StringBuilder class. Specifically, given StringBuilder sb, I'd like sb += "text" to become equivalent to sb.Append("text"). Here's the syntax for creating an extension…
Jude Allred
  • 10,577
  • 7
  • 25
  • 27
171
votes
5 answers

How can I reliably get an object's address when operator& is overloaded?

Consider the following program: struct ghost { // ghosts like to pretend that they don't exist ghost* operator&() const volatile { return 0; } }; int main() { ghost clyde; ghost* clydes_address = &clyde; // darn; that's not clyde's…
James McNellis
  • 327,682
  • 71
  • 882
  • 954
156
votes
14 answers

What makes Scala's operator overloading "good", but C++'s "bad"?

Operator overloading in C++ is considered by many to be A Bad Thing(tm), and a mistake not to be repeated in newer languages. Certainly, it was one feature specifically dropped when designing Java. Now that I've started reading up on Scala, I find…
skaffman
  • 381,978
  • 94
  • 789
  • 754
1
2 3
99 100