#include <iostream>
using namespace std;
class A {
public:
A(string name):name_(name) {}
private:
friend class B;
void Print() { cout << "A" << endl; }
string name_;
};
class B {
public:
B(A* a): a_(a) {}
void Print() {
a_->Print();
cout << "a.name:" << a_->name_ << endl;
}
private:
A* a_;
};
int main() {
A a("A");
B b(&a);
b.Print();
}
输出:
A
a.name:A