C++11中,friend的使用更为方便
class Poly;
typedef Poly P;
class LiLei
{
friend class Poly; // C++98 - 通过, C++11 - 通过
};
class Jim
{
friend Poly; // C++98 - 失败, C++11 - 通过 C++11中不需要使用类
};
class HanMeiMei
{
friend P; // C++98 - 失败, C++11 - 通过 C++11中甚至可以使用别名
};
虽然是简单的改动,但此时,我们可以为类模板声明友元了。下面看一个例子
// 为了方便测试,做了危险的定义
#ifdef UNIT_TEST
#define private public
#endif
class Defender
{
public:
void Defence(int x, int y) {}
void Tackle(int x, int y) {}
private:
int pos_x = 15;
int pos_y = 0;
int speed = 2;
int stamina = 120;
};
class Attacker
{
public:
void Move(int x, int y) {}
void SpeedUp(float ratio) {}
private:
int pos_x = 0;
int pos_y = -30;
int speed = 3;
int stamina = 100;
};
#ifdef UNIT_TEST
class Validator
{
public:
void Validate(int x, int y, Defender & d) {}
void Validate(int x, int y, Attacker & a) {}
};
int main()
{
Defender d;
Attacker a;
a.Move(15, 30);
d.Defence(15, 30);
a.SpeedUp(1.5f);
d.Defence(15, 30);
Validator v;
v.Validate(7, 0, d);
v.Validate(1, -10, a);
return 0;
}
#endif
代码
#define private public
的坏处显而易见,它不但降低了可读性,并且如果声明成员函数或成员变量时,不使用 private或public,默认是private。
接下来看看使用friend的类模板
template class DefenderT
{
public:
friend T;
void Defence(int x, int y) {}
void Tackle(int x, int y) {}
private:
int pos_x = 15;
int pos_y = 0;
int speed = 2;
int stamina = 120;
};
template class AttackerT
{
public:
friend T;
void Move(int x, int y) {}
void SpeedUp(float ratio) {}
private:
int pos_x = 0;
int pos_y = -30;
int speed = 3;
int stamina = 100;
};
//按照C++的定义,Defender和Attacker不会有友元,因而保持了很好的封装性
using Defender = DefenderT; // 普通的类定义,使用int做参数
using Attacker = AttackerT;
#ifdef UNIT_TEST
class Validator
{
public:
void Validate(int x, int y, DefenderTest & d) {}
void Validate(int x, int y, AttackerTest & a) {}
};
using DefenderTest = DefenderT; // 测试专用的定义,Validator类成为友元,可任意访问
using AttackerTest = AttackerT;
int main()
{
DefenderTest d;
AttackerTest a;
a.Move(15, 30);
d.Defence(15, 30);
a.SpeedUp(1.5f);
d.Defence(15, 30);
Validator v;
v.Validate(7, 0, d);
v.Validate(1, -10, a);
return 0;
}
#endif