全局函数做友元
#include <iostream>
class Person
{
//print是Person朋友 可以访问类中私有内容
friend void print(Person &person);
public:
Person(int age):
age(age),
sex(0)
{
}
public:
int age;
private:
int sex;
};
void print(Person &person)
{
std::cout << person.age << std::endl;
std::cout << person.sex << std::endl;
}
int main()
{
Person p(10);
print(p);
return 0;
}