我们知道如果要访问类的私有成员变量,调用类的公有成员函数是唯一办法,而类的私有成员函数则无法访问。
友元提供了另一种访问私有成员的方案,友元有三种:
1.友元全局函数
2.友元类
3.友元成员函数
1.友元全局函数
在友元全局函数中可以访问另一个类的所有成员。
#include<iostream>
using namespace std;
class CGirl
{
public:
string m_name;
CGirl() { m_name = "zhongge"; m_xw = 87; }
void showname() { cout << "name is : " << m_name << endl; }
private:
int m_xw;
void showxw() { cout << "xiongwei is " << m_xw<<endl; }
};
int main() {
CGirl g;
g.showname();
g.showxw();
return 0;
}
报错了,无法访问私有成员函数。
把main()函数声明为他的友元,main就可以访问他的私有成员了;
#include<iostream>
using namespace std;
class CGirl
{
friend int main();//这行声明友元放哪都行
public:
string m_name;
CGirl() { m_name = "zhongge"; m_xw = 87; }
void showname() { cout << "name is : " << m_name << endl; }
private:
int m_xw;
void showxw() { cout << "xiongwei is " << m_xw<<endl; }
};
int main() {
CGirl g;
g.showname();
g.showxw();
return 0;
}
2.友元类:
在友元类的所有成员函数中,都可以访问另一个类的所有成员。
class CGirl
{
friend int main();
friend class CBoy;
//声明CBoy它为友元类,CBoy就可以随便访问CGirl的成员了!!!
public:
string m_name;
CGirl() { m_name = "zhongge"; m_xw = 87; }
void showname() const{ cout << "name is : " << m_name << endl; }
private:
int m_xw;
void showxw() const{ cout << "xiongwei is " << m_xw<<endl; }
};
友元类的注意事项:
.友元类不能被继承
.友元关系是单向的,不具备交换性。
3.友元成员函数:
在友元类某成员函数中,可以访问另一个类的所有成员。
#include<iostream>
using namespace std;
class CGirl;
class CGirl
{
public:
string m_name;
CGirl() { m_name = "zhongge"; m_xw = 87; }
void showname() const{ cout << "name is : " << m_name << endl; }
private:
int m_xw;
void showxw() const{ cout << "xiongwei is " << m_xw<<endl; }
};
class CBoy {
public:
void func1(const CGirl& g) {
cout << "func()1 my girlfriend'xw is" << g.m_xw << endl;
}
void func2(const CGirl& g) {
cout << "func2() my girlfriend'xw is" << g.m_xw << endl;
}
};
int main() {
CGirl g;
CBoy b;
b.func1(g);
return 0;
}
现在CBoy与CGirl没有任何关系,我们要是把CBoy类的func1()func2()设置为超女类的友元函数;
分四步:
1.把超女类的声明前置
class CGirl;
2.把男朋友类的定义放在超女类之前;
class CGirl;
class CBoy {
public:
void func1(const CGirl& g) {
cout << "func()1 my girlfriend'xw is" << g.m_xw << endl;
}
void func2(const CGirl& g) {
cout << "func2() my girlfriend'xw is" << g.m_xw << endl;
}
};
3.把男朋友的成员函数的函数体从类的定义中拿出来,放在定义超女类的后面。
class CGirl;
class CBoy {
public:
void func1(const CGirl& g) ;
void func2(const CGirl& g) ;
};
class CGirl
{
public:
string m_name;
CGirl() { m_name = "zhongge"; m_xw = 87; }
void showname() const{ cout << "name is : " << m_name << endl; }
private:
int m_xw;
void showxw() const{ cout << "xiongwei is " << m_xw<<endl; }
};
void func1(const CGirl& g) {
cout << "func()1 my girlfriend'xw is" << g.m_xw << endl;
}
void func2(const CGirl& g) {
cout << "func2() my girlfriend'xw is" << g.m_xw << endl;
}
4.把男朋友类的成员函数声明为超女类的成员函数。
class CGirl;
class CBoy {
public:
void func1(const CGirl& g) ;
void func2(const CGirl& g) ;
};
class CGirl
{
friend void CBoy::func1(const CGirl&g);
friend void CBoy::func2(const CGirl&g);
public:
string m_name;
CGirl() { m_name = "zhongge"; m_xw = 87; }
void showname() const{ cout << "name is : " << m_name << endl; }
private:
int m_xw;
void showxw() const{ cout << "xiongwei is " << m_xw<<endl; }
};
void func1(const CGirl& g) {
cout << "func()1 my girlfriend'xw is" << g.m_xw << endl;
}
void func2(const CGirl& g) {
cout << "func2() my girlfriend'xw is" << g.m_xw << endl;
}