friend
#include <iostream> using namespace std;
//friend 友元,效率的问题
//get 方法和set方法,是标准封装的结果,friend破坏了这种封装。但又带来效率的提高。但又带来了效率的提高。
//有时需要定义一些函数,这些函数不是类的一部分,但又需要频繁地访问类的数据成员,这时可以将这些函数定义为该类的友元函数
class Sprite
{
friend void fight(Sprite& sp);
public:
Sprite(int lb):_lifeBlood(lb){}
int getLifeblood()
{
return _lifeBlood;
}
void setLifeblood(int lb)
{
_lifeBlood = lb;
}
private:
int _lifeBlood;
}; void fight(Sprite& sp)
{
// sp.setLifeblood(sp.getLifeblood() - 20);
// cout << sp.getLifeblood();
sp._lifeBlood = sp._lifeBlood - ;
cout << sp._lifeBlood << endl;
}
int main()
{
Sprite sp();
fight(sp);
return ;
}
//声明为谁的友元就可以通过谁的对象访问谁的私有成员
#include <iostream>
using namespace std;
class Complex
{
friend Complex operator+(Complex &c1, Complex &c2);
public:
Complex(double r = , double i = ):real(r), image(i){}
void dumpFormat()
{
cout << "(" << real << "," << image << ")";
}
private:
double real;
double image;
}; Complex operator+(Complex &c1, Complex &c2)
{
Complex c;
c.real = c1.real + c2.real;
c.image = c1.image + c2.image;
return c;
} int main()
{
Complex sum;
Complex c1(, );
Complex c2(, );
sum = c1 + c2;
sum.dumpFormat();
return ;
}
全局函数作友元
#include <iostream>
#include <math.h>
using namespace std; class Point
{
friend float getdistance(const Point& p1, const Point& p2);
public:
Point(int x = , int y = ):_x(x), _y(y){}
void dumpFormat()
{
cout << "_x" << _x << "_y" << _y;
}
private:
float _x;
float _y; };
float getdistance(const Point& p1, const Point& p2)
{
float dx = p1._x - p2._x;
float dy = p1._y - p2._y;
return sqrt(dx * dx + dy * dy);
}
int main()
{
Point p1(, );
p1.dumpFormat();
Point p2(, );
p2.dumpFormat();
cout << "dis:" << getdistance(p1, p2);
return ;
}
类成员做友元
#include <iostream>
#include <math.h>
using namespace std;
class Point;//前向声明的问题,前向声明是一种不完全类型的声明,不能定义对象,可以定义指针和引用做参数和返回值,仅用在函数声明
class ManagePoint
{
public:
float getdistance(const Point &p1, const Point &p2);
};
class Point
{
friend float ManagePoint::getdistance(const Point& p1, const Point& p2);
public:
Point(int x = , int y = ):_x(x), _y(y){}
void dumpFormat()
{
cout << "_x" << _x << "_y" << _y;
}
private:
float _x;
float _y; }; float ManagePoint::getdistance(const Point &p1, const Point &p2)
{
float dx = p1._x - p2._x;
float dy = p1._y - p2._y;
return sqrt(dx * dx + dy * dy);
}
int main()
{
Point p1(, );
p1.dumpFormat();
Point p2(, );
p2.dumpFormat();
ManagePoint m;
cout << "dis:" << m.getdistance(p1, p2);
return ;
}
友元类
实际工作中程序员喜欢友元类
#include <iostream>
#include <math.h>
using namespace std; class Point
{
friend class ManagePoint;
public:
Point(int x = , int y = ):_x(x), _y(y){}
void dumpFormat()
{
cout << "_x" << _x << "_y" << _y;
}
private:
float _x;
float _y; };
class ManagePoint
{
public:
float getdistance(const Point &p1, const Point &p2);
};
float ManagePoint::getdistance(const Point &p1, const Point &p2)
{
float dx = p1._x - p2._x;
float dy = p1._y - p2._y;
return sqrt(dx * dx + dy * dy);
}
int main()
{
Point p1(, );
p1.dumpFormat();
Point p2(, );
p2.dumpFormat();
ManagePoint m;
cout << "dis:" << m.getdistance(p1, p2);
return ;
}
随机推荐
- ubuntu server 18.04 单机安装openstack
https://ubuntu.com/openstack/install#workstation-deployment sudo snap install microstack --classic - ...
- 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_09-前端显示当前用户-需求分析
登陆成功 应该要显示用户的信息 cookie只存了用户的身份令牌.不包含用户的信息 拿着短令牌 请求认证服务获取到jwt.然后存储到sessionStorage 1.用户请求认证服务,登录成功. 2. ...
- Linux -- Proactor(及其与Reactor的比较)
高并发服务器常由多线程+IO复用服务器(one event loop per thread) 两种I/O多路复用模式:Reactor和Proactor 一般地,I/O多路复用机制都依赖于一个事件多路分 ...
- python命令行参数解析OptionParser类用法实例
python命令行参数解析OptionParser类用法实例 本文实例讲述了python命令行参数解析OptionParser类的用法,分享给大家供大家参考. 具体代码如下: from opt ...
- (转)Flink简介
1. Flink的引入 这几年大数据的飞速发展,出现了很多热门的开源社区,其中著名的有 Hadoop.Storm,以及后来的 Spark,他们都有着各自专注的应用场景.Spark 掀开了内存计算的先河 ...
- 给.Net Core添加Swagger实现接口文档自动生成
1.添加Nuget相关引用 Swashbuckle.AspNetCore
- vue等单页面应用及其优缺点
优点: Vue 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件,核心是一个响应的数据绑定系统.MVVM.数据驱动.组件化.轻量.简洁.高效.快速.模块友好. 缺点: 不支持低版本 ...
- git 提交大小超过100M
#MsnDialog.ad, #MyMoveAd, #QQ_Full, #ad-SNSSplashAd, #ad6cn, #adBody07, #adLeftFloat, #adRightFloat, ...
- 如何创建一个线程安全的Map?
1,使用普通的旧的Hashtable HashMap允许null作为key,而Hashtable不可以 2,使用Collections中同步化的包装方法synchronizedMap 3,使用conc ...
- NLog文章系列—系列文章目录以及简要介绍
参考文章:http://www.cnblogs.com/dflying/archive/2006/12/04/581750.aspx