#include <iostream>
using namespace std;
//const c 冒牌货
//register cpu 身边的小太监
//typdef 混号王
class Test2
{
public:
//有元函数 特点:有一个参数是友元类的指针或引用
friend int opNum(Test2* p,int a);//函数声明 也可以放到protected ,private 中
Test2(int a,int b)
{
this->a=a;
this->b=b;
}
int getA()
{
return this->a;
}
private:
int a;
int b;
};
int opNum(Test2 *p,int a)
{
p->a=a;//这里可以操作私有变量 这个函数是Test2 的好朋友
return 0;
}
void main()
{
Test2 t1(1,2);
t1.getA();
opNum(&t1,10);
printf("%d\n",t1.getA());
system("pause");
}
B是A 的好朋友
本文深入探讨了C++中友元类的概念及其如何实现对私有成员的操作,通过实例展示了友元类在实际编程中的应用。
956

被折叠的 条评论
为什么被折叠?



