友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。
Serializable Person类定义了一个它自己的友元,如清单1所示。
Consider a Serializable Person class that defines a friend of its own, as shown in Listing 1.
名为spacefriend的友元函数,它是space类的友元,它的参数是类对象,输出类对象的x变量的值。
A friend function named spacefriend, which is a friend of class space, and which takes the class object as an argument and prints the value of its variable x.
为了访问一个用户定义的类里的私有数据,你必须声明全局函数为友元函数,并且在类中定义。
But to be able to reach any private data within a user defined class you have to declare the global function as a friend within the definition of the class.
应用推荐