友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。类授予它的友元特别的访问权。通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其它部分的拥有者的同意)。
为什么这不可以友元函数访问私有变量?
Why can't this friend function access the private variables?
要求:分别用成员函数和友元函数来求圆的面积和周长。
Requirements: member function respectively, and a friend function to find area and circumference of a circle.
友元函数用来访问类的非公有成员。友元函数不是类的成员函数。
A friend function is used for accessing the non-public members of a class.
应用推荐