185. Friend Function

Question.1

A friend function accesses a private member:

class Box {
   int width;
public:
   Box(int w) : width(w) {}
   friend void printWidth(const Box& b);
};

void printWidth(const Box& b) {
   printf("%d", b.width); // Access private!
}

Box box(10);
printWidth(box);

Will b.width compile inside printWidth?

Need Help? Refer to the Quick Guide below

Select Answer