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 ;
}
随机推荐
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_05-Feign远程调用-客户端负载均衡介绍
2 Feign远程调用 在前后端分离架构中,服务层被拆分成了很多的微服务,服务与服务之间难免发生交互,比如:课程发布需要调用 CMS服务生成课程静态化页面,本节研究微服务远程调用所使用的技术. 下图是 ...
- 浅谈-对modbus的理解
浅谈-对modbus的理解 一.简介 Modbus由MODICON公司于1979年开发,是一种工业现场总线协议标准.1996年施耐德公司推出基于以太网TCP/IP的Modbus协议:ModbusTCP ...
- Python基于正则表达式实现文件内容替换的方法
Python基于正则表达式实现文件内容替换的方法 本文实例讲述了Python基于正则表达式实现文件内容替换的方法.分享给大家供大家参考,具体如下: 最近因为有一个项目需要从普通的服务器移植到SAE,而 ...
- MediaElement 不能显示的问题
1.记得在窗体的 Load 事件里面调用 Player1.Play();方法,就可以在窗体加载后就直接播放视频 2.WPF MediaElement其实和Windows系统自带的播放器是一样的,因此W ...
- 【VS开发】【数据库开发】windows下libevent x64库静态编译
按照libevent的文档,使用VC的nmake -f Makefile.nmake即可编译32位release模式.因为项目中要求编译64位的版本,需要在Makefile.nmake中添加一个LIB ...
- 基于MSP430F2618的程控电压源
基于MSP430F2618的程控电压源 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 系列博客说明:此系列博客属于作者在大三大四阶段所储备的关于电子电路设计 ...
- RH124-3 目录结构_转
在linux里安装的时候,是可以指定某分区装在某文件夹里 目录意义 /bin 存放命令,不可以在装系统单独挂载分区 /home /dev 存放硬件设备 不可以单独挂载分区 /boot 500M 和系统 ...
- Haystack--基于Django的全文检索框架
好文章转载自:https://suguangti.cnblogs.com/p/11167097.html 阅读目录 1.什么是Haystack 2.安装 3.配置 4.处理数据 创建索引 5.设置视图 ...
- 关于Django ModelForm渲染时间格式问题
关于Django ModelForm渲染时间格式问题 直接定义DateTimeInput或者DateTimeFile是不行的,渲染在html页面中的仍然是Input text类型 解决办法:自定义小部 ...
- Django入门(下)
一.创建APP 在每一个django项目中可以包含多个APP,相当于一个大型项目中的分系统.子模块.功能部件等.互相之间比较独立,但也有联系. 在pycharm下方的Terminal终端中输入命令: ...