I believe it is because when you define the operators, you define what the right side should be (and only the right side). If you make it a friend, then I think it lets other types (or maybe just the + operator itself) use the function by switching variables around where neccesary. Although I would also suggest doing some searching because I'm not sure if this is exactly correct.
For any given binary operator, it the left hand object that carries out the operation.
Say we have a class ClassX which has an overload for the + operator for integers.
Then if we create an object of thet class (call it objx), then we can say objx + 2. objx will will carry out the operation and return the result object of ClassX.
But what happens if we say 2 + objx ?
The result should be the same as 2 + objx - BUT integers are not classes and certainly numeric literals are not - so 2+objx is not possible directly.
It would be a pain if we had to design our code to make sure we always
has ojbjects of ClassX on the left hand side of the + whenever we wanted to add an int to it.
This is where we use friend overloads for operator+ for ClassX.
1 2 3 4 5 6
class ClassX
{
friend ClassX operator+(ClassX objx, int i);
friend ClassX operator+(int i, ClassX objx);
};
Thank you for the replies. I understand the need for symmetric operators. Now consider the case where I have a class Vector2D for which I wish to implement operator +, i.e. either
In general, do not overload operators. The assignment operator (operator=), in particular, is insidious and should be avoided. You can define functions like Equals() and CopyFrom() if you need them.
However, there may be rare cases where you need to overload an operator to interoperate with templates or "standard" C++ classes (such as operator<<(ostream&, const T&) for logging). These are acceptable if fully justified, but you should try to avoid these whenever possible. In particular, do not overload operator== or operator< just so that your class can be used as a key in an STL container; instead, you should create equality and comparison functor types when declaring the container.
I think if your a new developer. Adopting a known style of code writing is going to be highly beneficial. Google's standard is pretty well designed.
Hmmmpf. I have to disagree with Google's stance on operator overloading. Are they saying that the current set of STL containers are the only containers one could ever need? Or that the STL is stupid, and why would anyone write data structures that integrated with it?
I find that if I have to define a new container, I either end up reinventing STL or I make my container conform to other STL containers/algorithms.
Especially for data types that are purely mathematical in nature, such as the above example, I personally wouldn't think twice about overloading +, -, +=, -=, etc since all of those operators have an agreed upon mathematical definition.
I would not, however, advocate overloading them for everything. For example, the mathematical operators overloaded for a stack do not make sense in my mind.
But you don't have to overload the operators. Their stance is more on code readability and maintainability. And in which regard, their stance does provide better code.
e.g
1 2 3
myclass += someclass;
// vs
myclass.add(someclass);
The code to develop the add() is going to be cleaner and easier to use/find than the operator. Esp as most IDEs will let you ctrl+click it to goto the implementation.
I would think they'd differ by one line of code -- one has a return at the end and the other probably doesn't.
If the decision came down to readability/maintainability vs. my IDE not handling one case, I'd choose the former every time. (Not everyone uses the same IDEs; I, for one, don't use any IDE).
I understand from talking to some others that Google also recommends against using templates, which explains the "However..." paragraph. Though I'm not sure why they would be against templates.
I understand from talking to some others that Google also recommends against using templates, which explains the "However..." paragraph. Though I'm not sure why they would be against templates.
You can do everything templates offer through a good OO design and inheritance. :)
I have to disagree with the first statement. Well, ok, anything can be done with software, so at some level you are right. But templates often simplify things or make things more robust. How, for example, would you build a generic container class that affords the same level of type safety as the STL containers without templates?
I find working in IDEs a lot like working inside a box. I use a graphical editor that I invoke from the command line. I find I can navigate just as quickly via command line as I could if I had to click up an down directory trees in the open file dialog. And I find a couple of things very annoying about IDEs:
1) auto-formatting /auto-entry of code. Even with the level of customization provided by the IDE, I can never make it auto-format my code just the way I like it.
2) auto parameter lookup. More often than not I know the parameters that have to be passed to the function and in what order, so the second or two the editor goes away parsing a ginormous ctags file is more annoying than helpful.
Hmm...I've never had a problem with either of those...and besides, couldn't you just turn off 1/2? I find that #2 rarely annoys me, since if I keep typing, my keystrokes are still queued even if the program is lagging trying to get the parameters.
I find that the Intellisense/variable completion actually speeds up some of my code writing (although I admit it sometimes annoys me when it gives me obvious garbage like "Can't find type" of something I defined right above it, not that it matters much though). Another thing I like is that (at least in VC++) I can simply look a global list of all my functions and click on whichever one I want to edit without have to scroll through 5000+ lines of code. (Or use find, which might find uses of the function within other code)
I suppose they could be turned, though at that point the IDE is nothing more than a text editor, since the practice is to use gdb for debugging and a well-placed "make" to build.
Problem is that the ctags file I think is several hundred MB and in general it can't even stay in the page cache because the application chews up most of RAM causing the page cache to be flushed.
jsmith: How often do you work in multi-developer teams though? Using a text editor and file based structure is nothing but tedious and alot of pointless work. Especially when your writing interfaces to other peoples code and your not familiar with it.
A simple ctrl+click on a function call to be taken to the definition far outweighs having to find the file the function is in, then load it and scroll down to it.
The project I work on has upwards of 50 developers and 1,000,000+ lines of code in several thousand source files.
When I'm interfacing to other people's code, chances are I'm not even sure of the method name, let alone the parameters, which makes intellisense useless.
I have to know what file the function is in anyway, because I have to include the header for it. In the event I don't, then grep works, often even better than intellisense because I can use regular expressions that help narrow down the hits.
IMHO the inconvenience of having to navigate a directory tree occasionally is far outweighed by my IDE going away for 2 seconds every time I write a function call, because more often than not I know the parameters. It doesn't need to tell me the parameters to string::length() or vector<>::begin(), etc, etc. But that the editor pauses continuously is annoying.