- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 256字
- 2021-06-10 18:28:14
Making friends
The problem with the code above is that the cartesian_vector class accesses private members of the point class. Since we have written both classes, we are happy to bend the rules, and so we make the cartesian_vector class a friend of the point class:
class cartesian_vector; // forward decalartion
class point
{
double x; double y;
public:
point(double x, double y) : x(x), y(y){}
friend class cartesian_point;
};
Since the cartesian_vector class is declared after the point class, we have to provide a forward declaration that essentially tells the compiler that the name cartesian_vector is about to be used and it will be declared elsewhere. The important line starts with friend. This indicates that the code for the entire class, cartesian_vector, can have access to the private members (data and methods) of the point class.
You can also declare friend functions. For example, you could declare an operator such that a point object can be inserted into the cout object, so it can be printed to the console. You cannot change the ostream class, but you can define a global method:
ostream& operator<<(ostream& stm, const point& pt)
{
stm << "(" << pt.x << "," << pt.y << ")";
return stm;
}
This function accesses the private members of point so you have to make the function a friend of the point class with:
friend ostream& operator<<(ostream&, const point&);
Such friend declarations have to be declared in the point class, but it is irrelevant whether it is put in the public or private section.