#include class Bar { friend class Foo; friend void setit(Bar& b, int n); int x; public: // friend function defined in the class // is a global function friend void setit(Bar& b, int n1, int n2) { b.x = n1 + n2; } }; class Foo { public: void printit(Bar b) { // we can access private members of Bar because class Foo is its friend std::cout << b.x << std::endl; } }; void setit(Bar& b, int n) { // we can access private members of Bar because this function is its friend b.x = n; } int main(int argc, char** argv) { Foo f; Bar b; setit(b, 25); f.printit(b); // call global function that is friend of class Bar setit(b, 13, 16); f.printit(b); return 0; }