Operator overloading

For a simple struct like
struct MyObject
{
int number1;
int number2;
}

Do I have to implement += operator if I already implemented operator + , if I like to use something like

obj1 += obj2?

What will be default behavior if I do not implement + and +=?

Thanks

Chris
closed account (48bpfSEw)
do it and you'll see it! ^^

the compiler says aproximately: operator + for type MyObject not defined.

I actually had +operator implemented, but forgot +=. It seems
obj1 += obj2 works just fine in LINUX, which make me confused ...

I do expect unexpected behavior without += but it seems not.
closed account (48bpfSEw)
oh, now I understood your question!

http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.26.2


your assumtion is correct.
Thanks. You mean it should be "unexpected behavior"?
That documentation is clearly for Java, which doesn't even have operator overloading.

No, operator+() and operator+=() are independent. If you want your class to support both, you need to define both overloads. If you only provide one overload and you try to use the one you didn't define, the compiler will simply reject the code, so it doesn't have any behavior. It's not even a valid program.

However, you can implement one in terms of the other:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class A{
    A operator+(const A &) const;
    const A &operator+=(const A &a){
        return *this = *this + a;
    }
};

class B{
    const B &operator+=(const B &b);
    B operator+(const B &b) const{
        B ret = *this;
        ret += b;
        return ret;
    }
};
Which one is better will depend on the class.
Last edited on
very interesting. I think the codes using += passed compilers of both MS VS11 and Gcc 4.8.*, even though I only implemented +. Somehow, it worked as if I implemented += in LINUX, though I think it could be unexpected behaviors.
This fails to compile on GCC 4.8.1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

//#define DEFINEME

class A{
    int a;
public:
    A(int a): a(a){}
    A operator+(const A &a) const{
        return A(this->a + a.a);
    }
    int get_a() const{
        return this->a;
    }
#ifdef DEFINEME
    const A &operator+=(const A &a){
        this->a += a.a;
        return *this;
    }
#endif
};

int main(){
    A a = 42;
    A b = 24;
    a += b;
    std::cout << a.get_a() << std::endl;
    return 0;
}
operator.cpp: In function 'int main()':
operator.cpp:26:4: error: no match for 'operator+=' (operand types are 'A' and 'A')

It compiles and runs successfully if line 3 is uncommented.

EDIT: As for what you're seeing, My only guess is that there's an overload outside the class. The above compiles if this global overload is added:
1
2
3
4
const A &operator+=(A &a, const A &b){
	a = a + b;
	return a;
}
Last edited on
Hi Helios,

my bad. You are right. It is overloaded by someone else and hide in a different place. I missed that.

Thanks a lot for your time to dig into this for me.

Chris
Topic archived. No new replies allowed.