Except yours won't work properly xErath.
When you do it should return a copy of the value before it is incremented. Yours makes a copy and then increments that without ever incrementing the actual object.
If you don't believe lets test:
Code:
#include <iostream>
class Foo {
int x_;
public:
Foo() : x_(0) {}
Foo(const Foo & f) : x_(f.x_) {}
const int & x() const { return x_; }
Foo operator++(int) // post increment
{
return ++Foo(*this);
}
Foo& operator++() // pre increment
{
x_ += 1;
return *this;
}
friend std::ostream& operator<<(std::ostream &o, const Foo &f)
{
return o<<f.x_;
}
};
int main()
{
Foo f;
std::cout<<f<<std::endl;
f++;
std::cout<<f<<std::endl;
++f;
std::cout<<f<<std::endl;
}
output:
Now if we change the function to:
Code:
Foo operator++(int) // post increment
{
Foo temp(*this);
++*this;
return temp;
}
we get the proper output of:
Operator+ is a binary operator.
Just like operator- there is a binary and an unary version. Normally the unary just returns a copy a copy of the object:
Code:
Foo Foo::operator+() const
{
return *this;
}
Of course it could easily be made a non friend, non member function.