C++ 友元的使用

C++ 友元的使用,第1张

简单例子一看就懂

#include
using namespace std;

class A {
	friend void text01();//友元
public:
	int a = 100;
private:
	int b = 200;
};

void text01()
{
	A q;
	cout << q.a << endl;;
	cout << q.b;//可以访问A类里面的私有信息
	
}
int main() {
	
	text01();
	

	return 0;

}
#include
using namespace std;

class A {
	friend class B;//友元
public:
	int a = 100;
private:
	int b = 200;
};

class B {
	A a;
public:
	B()
	{   
		cout << a.a << endl;
		cout << a.b << endl;//友元访问私有
	}
	

};
void text01()
{
	B b;
	
	
}
int main() {
	
	text01();
	

	return 0;

}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/793910.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-06
下一篇2022-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存