Sometimes a non-member function may need access to private members, which it accepts as a tool. A function friendis a non-member function that provides private access to the classes it is friends with.
Class X{
int number;
public:
X& operator+=(const X& rhs);
friend int& operator+=(int& addthis, const X& rhs);
}
X& operator+=(const X& rhs){
this->number = this->number + rhs.number;
}
int& operator+=(int& addthis, const X& rhs){
addthis = addthis + rhs.number;
return *this;
}
I read that if I wanted to do +=for an object object += int, just reload the operator +=, but what if it intappeared in front of the object. Tell int += objectme Than I would have to write it as a function friend, and where I get a little lost. Why can't I write int& operator+=(int& addthis, const X& hrs);as a member function? I believe that a function friendcan work with other classes, since it is a non-member function, so it is not assigned to any particular class?
, , friend, -. , .