Hello, I am currently following a book, about SDL2 game development and there is a part that I cant quite understand...
This is the code.My question is about the operator overloads.
1.Why is the += a friend function in order to work? If I remove the friend key word it gives me an error "Too many arguments for this operator function".
2.Why, if I place the function body in the .cpp file I cant use the variable name m_x or m_y in it.
1. Yes, it is indeed nonsense (though allowed) to write it so. friend makes it a free function. Removing friend means it becomes a member function where it is not allowed to have two parameter for this function. Normally you write the (non friend) function like so:
Thanks. I tried the code you showed and it worked the same way.
So is the way you showed me the way to do the operator overload?
And because I don't know what I don't know :D about C++. I use this book as a guidance for finding new operators, functions or styles of coding on c++, when I saw operator overloading it was a new thing for me.In stackoverflow and other websites it was explained in some way, but I never saw it done like this and I got confused.