I've written a little sample code below.
#include <iostream>
#include <cstdio>
using namespace std;
class Box{
int l; //length
int b; //breadth
int h; //height
public:
Box(){ l =0; b = 0; h = 0;}
Box(int d1, int d2, int d3){ l = d1;b=d2;h=d3;}
int getLength(){return l;}
friend Box operator+(Box b1, Box b2){
Box tempBox;
tempBox.l = b1.l + b2.l;
tempBox.b = b1.b + b2.b;
tempBox.h = b1.h + b2.h;
return tempBox;
}
int calculateVolume(){ return l * b * h;}
};
This code doesn't produce errors when compiled/run. We can also change the friend function to the following:
friend Box operator+(Box &b1, Box &b2){
Box tempBox;
tempBox.l = b1.l + b2.l;
tempBox.b = b1.b + b2.b;
tempBox.h = b1.h + b2.h;
return tempBox;
and the test code runs equally well. My question is, what is the purpose of referencing the Box objects 'b1' and 'b2' by their memory addresses like in the second version of the friend function, something I see often in sample code? More to the point, if we're passing addresses to the friend function, how does it know to manipulate the objects stored at those addresses without any sort of dereferencing? Sorry, I'm a bit new to all this. Any help would be appreciated!