Linked Questions
1,394 questions linked to/from What are the basic rules and idioms for operator overloading?
289 votes
6 answers
512k views
How can I properly overload the << operator for an ostream? [duplicate]
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 six months and in between I upgraded my ...
109 votes
6 answers
141k views
operator << must take exactly one argument [duplicate]
a.h #include "logic.h" ... class A { friend ostream& operator<<(ostream&, A&); ... }; logic.cpp #include "a.h" ... ostream& logic::operator<<(ostream& os, A& a) {...
116 votes
5 answers
101k views
How can I overload the operator++ in two different ways for postfix a++ and prefix ++a? [duplicate]
How can I overload the operator++ in two different ways for postfix a++ and prefix ++a?
90 votes
8 answers
60k views
Why must the copy assignment operator return a reference/const reference? [duplicate]
In C++, the concept of returning reference from the copy assignment operator is unclear to me. Why can't the copy assignment operator return a copy of the new object? In addition, if I have class A, ...
52 votes
13 answers
11k views
Could I use operator == if I only implemented operator <? [duplicate]
I have implemented operator< for a certain object. Logically, if !(a < b) and !(b < a) it means a == b. Is this inferred automatically? Can I use == if I only implement <?
150 votes
2 answers
121k views
Operator overloading : member function vs. non-member function? [duplicate]
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 ...
64 votes
7 answers
37k views
How to define operator< on an n-tuple that satisfies a strict weak ordering [duplicate]
How to define operator< on n-tuple (for example on 3-tuple) so that it satisfy strict weak ordering concept ? I know that boost library has tuple class with correctly defined operator< but for ...
89 votes
2 answers
80k views
How to overload unary minus operator in C++? [duplicate]
I'm implementing vector class and I need to get an opposite of some vector. Is it possible to define this method using operator overloading? Here's what I mean: Vector2f vector1 = -vector2; Here's ...
60 votes
4 answers
81k views
Overloading ++ for both pre and post increment [duplicate]
Can we overload operator++ for pre-increment and post-increment? i.e. calling SampleObject++ and ++SampleObject gives the correct results. class CSample { public: int m_iValue; // just to ...
54 votes
5 answers
66k views
return value of operator overloading in C++ [duplicate]
I have a question about the return value of operator overloading in C++. Generally, I found two cases, one is return-by-value, and one is return-by-reference. So what's the underneath rule of that? ...
79 votes
3 answers
109k views
Operator overloading outside class [duplicate]
There are two ways to overload operators for a C++ class: Inside class class Vector2 { public: float x, y ; Vector2 operator+( const Vector2 & other ) { Vector2 ans ; ...
54 votes
4 answers
73k views
How do I create and use a class arrow operator? [duplicate]
So, after researching everywhere for it, I cannot seem to find how to create a class arrow operator, i.e., class Someclass { operator-> () /* ? */ { } }; I just need to know how to work ...
41 votes
5 answers
19k views
C++ equal(==) overload, Shortcut or best way comparing all attributes [duplicate]
I have to overload a == operator in C++ for a class with many attributes. The operator should return true, if and only if all attributes are equal. A shortcut might be useful, if these attributes ...
44 votes
5 answers
86k views
Comparison operator overloading [duplicate]
Which is best practice (in this case): bool Foo::operator==(const Foo& other) { return bar == other.bar; } // Implementation 1 bool Foo::operator!=(const Foo& other) { return bar != ...
58 votes
2 answers
9k views
In C++ do you need to overload operator== in both directions? [duplicate]
Say I am working with a class: class Foo{ public: std:string name; /*...*/ }/*end Foo*/ and I provide an overload for operator== bool operator==(const Foo& fooObj, const std::string& ...