为什么这不可以友元函数访问私有变量?
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.
为了访问一个用户定义的类里的私有数据,你必须声明全局函数为友元函数,并且在类中定义。
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.
名为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.
名为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.
应用推荐