• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ Can some expert help explain some feature of this code please.


New Coder
point operator+ (point& p1, point& p2){ // why do we use + here?
point sum = {p1.x + p2.x, p1.y + p2.y}
return sum;
o stream& operator<< (ostream& out, const point& p){ // why do we use & here?
out<< "(" << p.x << "," << p.y << ")";
return out;

Krusty the Senile

Active Coder
This is an example of operator overloading. In C++, you can define the behviour of some symbol based operations (the arithmetic ones as well as pointers) that make sense. In this case, operator is a keyword in C++.

Typically when dealing with the "+" symbol you can only perform that operation with primitives (int, char, double, float, etc) so in this case the "+" operator is being overloaded/defined to deal with "point" instances.

example usage:

// example points
point a(3, 3), b(4, 5);

// with the overloaded operator above, you could write:
point c = a + b;
// This is similar to:
point c = operator+(a, b);
In relation to the "&" symbol: this is a way of passing parameters to a function. There are 3 ways to do this: by copy, by pointer, and the ampersand is by reference:

void func(point p); // is by copy
void func(point *p); // is by address/pointer
void func(point &p); // is by reference

// lets say you have:
point aPoint;

// you would use the first one with
func(aPoint); // this would create a copy of the aPoint parameter and use that
// the second one:
func(&aPoint); // this takes the address of the aPoint parameter and modifies it
// the third one:
func(aPoint); // this is similar to the second one but you don't have to pass the address of the aPoint variable
I think there are some other things about passing by reference over passing by pointer (one that comes to mind is with operator overloading) but you'll have to look into that further. You could potentially do something like:

point operator+(point &a, point &b);
// but to use this would look something like:
point c = &a + &b; // which looks whacky
// or
point operator+(point a, point b);
// and you could use
point c = a + b; // but typically you would want to reduce copy operations and work directly with object references.
Object cloning/copying is something to look into and consider, but that's a whole other topic.
I'll leave it at that for now.

Krusty the Senile

Active Coder
I think operator overloading was done for C++ to make certain arithmetic operations readable, when it makes sense. The point class is a good example: there are operations you can perform on points (add, subtract, multiply - but not what you'd expect) where a "shorthand" would become more readable. eg.

z = a + b + c + d + e;


z = addPoints(addPoints(addPoints(addPoints(a, b), c), d), e);

Note that the "stream" operators "<<" and ">>" are implemented as overloaded operators.

I haven't come across usages in real world (but I haven't done much C++ outside of Microsoft's offering, or recently). The other "disadvantage" to operator overloading is either understanding or assuming the behaviour an overloaded operator would take. eg. multiplying 2 points can either be matrix multiplication or vector multiplication: it all depends on context.


New Coder
Ok I get it in theory but haven't seen it yet in code commercially. Seems to be a world of difference between what this school teaches and what is use s it the real world. Much thanks again eeflores. What do you code in these days. Did you learn C# through Microsoft?

Krusty the Senile

Active Coder
A couple of examples: https://github.com/id-Software/Quake-III-Arena/blob/master/code/splines/math_angles.h has operator overloading for a matrix class. There is something similar in the Blender source code, but there's also a vector class with operator overloading: https://github.com/sobotka/blender/blob/master/intern/itasc/kdl/frameacc.hpp . This is something that immediately comes to mind for C++ (graphics classes and all the math that goes into manipulating the objects). I was trying to see if the unreal engine source had examples too, but it's locked away.

The boost C++ library (https://www.boost.org/) are a set of functions and classes that attempt to add support for constructs that are found in other languages. There seems to be an operators header file (https://www.boost.org/doc/libs/1_39_0/libs/utility/operators.htm) which may give examples of how overloaded operators may work.

I think I've seen somewhere a network C++ library that uses the stream operators to send/receive data, so you'd have something like: server << data;. Google search turned up https://isocpp.org/wiki/faq/input-output#serialization-xref-communication .

I guess the takeaway for now is to know that they exist and that C++ programmers out there may be using them.

Anyways, I'm mainly doing Java these days. I think I was able to transition to C# based on what I knew from Java. If you get a chance, I think it's good and easy to get Visual Studio, fire up a Windows Forms project, draw up a window or dialog with buttons and labels and add event functions - good fun!


Just a reminder, make sure you are placing all your code in the dedicated BBcode feature! It makes it easier to look over. You can do this by clicking the (...) within the editor and selecting code.
Top Bottom