0

I just learnt the basics of operator overloading. After which I wrote the following code for vector addition of two points in a plane.

#include <bits/stdc++.h>
using namespace std;

struct point{
    int x, y;
    point operator+(point b){
        point c;
        c.x = x + b.x;
        c.y = y + b.y;
        return c;
    }
};

int main()
{
    point a, b, c;
    a.x = 1, a.y = 2, b.x = 3, b.y = 4;
    c = a + b;
    cout<<c.x <<" "<< c.y;
    return 0;
}

However most of the other operator overloading examples I find are coded very differently, example answers to this question. Even though I am getting the correct output using this, is there a cause for concern when I do it this way by declaring a new variable, or leaving out const?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
NUMBART
  • 52
  • 9
  • 5
    Unrelated to your problem but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Also please don't put multiple expressions in a single line separated by commas, that makes the code hard to read, understand and maintain. – Some programmer dude Jan 27 '20 at 08:26
  • Which version of C++ are You using? – Robert Andrzejuk Jan 27 '20 at 08:28
  • More related to your problem, while [this canonical implementation reference](https://en.cppreference.com/w/cpp/language/operators#Canonical_implementations) could be useful (especially the part about [binary arithmetic operators](https://en.cppreference.com/w/cpp/language/operators#Binary_arithmetic_operators)), that's not the only way to create and implement overloaded operators. – Some programmer dude Jan 27 '20 at 08:28
  • Related: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/) – Remy Lebeau Jan 27 '20 at 08:29
  • 3
    Side note: The way you do (`point b`) `b` already *is* a copy of the parameter passed. So you actually could just add x and y to b and return that one: `b.x += x; b.y += y; return b;`. – Aconcagua Jan 27 '20 at 08:31
  • Related: [What kind of optimization does const offer in C/C++?](https://stackoverflow.com/questions/27466642/what-kind-of-optimization-does-const-offer-in-c-c) – Robert Andrzejuk Jan 27 '20 at 08:33
  • Thanks @Some programmer dude, would the a and b variables in different lines have been a better idea? – NUMBART Jan 27 '20 at 08:34
  • I am using C++14 @Robert – NUMBART Jan 27 '20 at 08:35
  • 1
    You might want to consider providing a constructor. That would make your code even simpler: `point a(1, 2); point b(3, 4);` – Aconcagua Jan 27 '20 at 08:37
  • The definition of variables is usually okay in a single line, as long as all variables are of the exact same type (so no mixing non-pointer with pointers for example), and don't initialize one variable but not some other. It's the mixing of different things that makes code harder to read. So `point a, b, c;` is okay, but `a.x = 1, a.y = 2, b.x = 3, b.y = 4;` is less okay for many. – Some programmer dude Jan 27 '20 at 08:42
  • Thanks @Aconcagua your comment helped me undersand why using += was not affecting the actual values in the examples – NUMBART Jan 27 '20 at 08:49
  • @RobertAndrzejuk in this case, passing by value is probably better than passing by const reference. Copying `2 * sizeof(int)` is probably less overhead than `sizeof(void*)` + indirection (in many implementations). – L. F. Jan 27 '20 at 08:51
  • The `operator+()` function should be const too since you're not changing `*this`. Like this: https://godbolt.org/z/NfanrX – Ted Lyngmo Jan 27 '20 at 08:57
  • Thanks Some programmer dude, Robert, Remy, Aconcagua, L.F., Ted for helping me out. Still going through the links, will add any follow up questions I have to this comment section. – NUMBART Jan 27 '20 at 09:22

0 Answers0