Oct 27, 2016 at 11:53pm UTC
I was searching to learn more about the friend function when I found this.
___ CODE ____
// friend functions
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle() {}
Rectangle(int x, int y) : width(x), height(y) {}
int area() { return width * height; }
friend Rectangle duplicate(const Rectangle&);
};
Rectangle duplicate(const Rectangle& param)
{
Rectangle res;
res.width = param.width * 2;
res.height = param.height * 2;
return res;
}
int main() {
Rectangle foo;
Rectangle bar(2, 3);
foo = duplicate(bar);
cout << foo.area() << '\n';
return 0;
}
At this line friend Rectangle duplicate(const Rectangle&);
const Rectangle&. The & means a pass by reference? Don't we put it before normally ?
Thanks for help !!
Oct 28, 2016 at 3:08am UTC
Oh ! Yes I think I got it. It's the Class name so there is no "Defined" identifier name is this example, that's the & seems like being after, but in fact, it's in front of the supposed identifier name ?