Hello! I am looking at this code and trying to understand what is happening.
How does the program "know" that we get BOX3 adding sizes of first 2 ones? Is it because the overloading function has object as parameter and after we created first 2 box objects and as we don't have measures for hte third box, but have the overloading function, that is the last possibility for procesor to use it???- no other options have been left ,so it is taken for granted ,sth like this? We obvioulsy can't use overloaded function before we created and initializated first 2 objects (box1 and box2). It does not look too clear to me, which box is which one, this-> or b. , box1 or box2? Many thanks in advance!!!
Line 67: The compiler processes the right hand side of the = sign first. Processing left to right, the compiler recognizes box1, then sees the + operator. This will generate a call to Box::operator + ( <obj> ). We could overload operator + with different kinds of objects, but in this case, the argument is another box (box2). So this resolves the call to the function at line 27. The result of the function at line 27 is a temporary box (box), which becomes the return value. The returned object (a copy of local variable box) is then assigned to Box3.
Inside operator +, this refers to the left hand side of the + operator (box1) while b refers to box2.
In this case, we have a global function, not a member function of CVector.
The compiler will look for a CVector::operator + first. Not finding that, it will look for a global function. The function at line 12 will match. Global operator functions require two arguments since there is no implicit object as there would be if operator + were a member function.