扩展的friend语法

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值