#include<iostream>
using namespace std;
class B;//前置声明 A中有B
class A
{
public:
int a;
B* b;
A(B*b):b(b),a(10){cout<<"A()"<<endl;}
};
class B//B中有A
{
public:
const A &ob;//一定要const,否则会报错。
B():ob(this){cout<<"B()"<<"A ob.a = "<<ob.a<<endl;}//this是B* const型;
B(B& b):ob(&b){cout<<"B(B&)"<<endl;}
};
int main()
{
B b;
// A a(&b);
getchar();
return 0;
}
结果: