来自「C语言相关程序」· 代码 · 共 55 行

TXT
55
字号
#include<iostream.h>
class A;
class B
{
public:
	int & geti();
	B();
	B(B&);
	~B();
	B& operator=(B&);
private:
	A *pA;
	friend class A;
};
class A
{
public:
	void addout(B &b)
	{
		cout<<"the address of A in B is: "<<b.pA<<endl;
	}
private:
	int i;
	friend int &B::geti();
};
void main()
{
	B b;
	A a;
	b.geti()=1;
	a.addout(b);
	cout<<"b.pA->i"<<b.geti()<<endl;
}
B::B()
{
	pA=new A;
}
B::B(B& b)
{
	pA=new A(*(b.pA));
}
B::~B()
{
	delete pA;
}

B& B::operator=(B& b)
{
	pA=new A(*(b.pA));
	return *this;
}
int & B::geti()
{
	return pA->i;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?