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

8452 questions
2440
votes
8 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
  • 219,715
  • 46
  • 258
  • 445
475
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
425
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
  • 9,052
  • 12
  • 37
  • 35
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
  • 464,522
  • 92
  • 875
  • 1,084
335
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
  • 10,637
  • 9
  • 41
  • 51
329
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
  • 25,378
  • 33
  • 125
  • 153
324
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
  • 55,810
  • 13
  • 122
  • 180
296
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,667
  • 2
  • 14
  • 17
280
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…
279
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
  • 32,883
  • 32
  • 90
  • 131
271
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
  • 25,844
  • 28
  • 99
  • 154
209
votes
11 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,637
  • 6
  • 25
  • 28
191
votes
7 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,977
  • 7
  • 28
  • 27
173
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
  • 348,265
  • 75
  • 913
  • 977
167
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,939
  • 4
  • 34
  • 48
1
2 3
99 100