class Person
{
public:
friend class A;
private:
int i = 1;
};
class A
{
public:
Person p;
void Say()
{
cout << p.i << endl;
}
};
int main()
{
A a;
a.Say();
}
https://www.runoob.com/cplusplus/cpp-friend-functions.html
友元函数:
class Person
{
public:
friend void Print(Person& p);
private:
int i = 1;
};
void Print(Person& p)
{
cout << p.i << endl;
}
int main()
{
Person p;
Print(p);
}
友元函数并不是成员函数