operator overloadin

A quick question, I've moved onto operator overlaoding, and I have noticed that sometimes the overlaod is with and & and sometimes not? Question is what are the rules for using the &

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 //overload pre incremennt
 Integer &operator ++();

 //overload post increment
 Integer operator ++(int);

 //Copy assignment
 Integer & operator =(const Integer &a);
 //Move assignment
 Integer & operator =(Integer &&a);

 //overlaod stream output operator
 friend std::ostream &operator <<(std::ostream &out, const Integer &obj){
        out << *obj.m_pInt;
        return out;
    }
As far as i know
& is call by reference and && is a condition.

search on google- call by reference vs call by value in c++
Last edited on
&& is a condition

or a right-hand reference, which is the case of move operator
Last edited on
Integer &operator ++();
increments a variable and returns a reference to the incremented variable

Integer operator ++(int);
copies a variable. increments it, and returns a copy of the new value (doesnt upset the original)

Integer & operator =(Integer &&a);
this is a move operator, you still need to use std::move() unless returning the value which will automatically perform the move. integers are a bad example, try it with a class object.

basic rules...
there aren't any really, each example performs diffferent functionality and its for you to choose the one you want.

eg; you're first example increments a variable in memory then returns a reference to that instead of copying it.

you're second example reads a variable, increments it and returns that modified copy, so its not a ref.

Hi thanks for your answers - Integer is a basic class that I have written. I will search the internet to see if I can find any further information on when to/not use &
Registered users can post here. Sign in or register to post.