#include<iostream>
using namespace std;
class A;
class B {
public:
void get(A &a) {
cout << a.Age << endl;//类内 Age报红线
}
//void get(A &a);
};
class A {
friend void B::get(A &a);
public:
A() {
Age = 15;
}
private:
int Age;
};
//类外 Age不会报红线
//void B::get(A &a) {
// cout << a.Age << endl;
//}
int main() {
A a;
B b;
b.get(a);
system("pause");
return 0;
}
类内实现Age报红线,类外实现Age不报红线。