当前位置: 首页 > news >正文

优秀的响应式网站专门做排行榜的软件

优秀的响应式网站,专门做排行榜的软件,闵行区教育学院,建筑人才网电话友元分为两部分内容 友元函数友元类 友元函数 问题&#xff1a;当我们尝试去重载operator<<&#xff0c;然后发现没办法将operator<<重载成成员函数。因为cout的输出流对象和隐含的this指针在抢占第一个参数的位置。this指针默认是第一个参数也就是左操作 数了。…

友元分为两部分内容

  1. 友元函数
  2. 友元类

友元函数

问题:当我们尝试去重载operator<<,然后发现没办法将operator<<重载成成员函数。因为cout的输出流对象和隐含的this指针在抢占第一个参数的位置。this指针默认是第一个参数也就是左操作
数了。但是实际使用中cout需要是第一个形参对象,才能正常使用。所以要将operator<<重载成全局函数。但又会导致类外没办法访问成员,此时就需要友元来解决。operator>>同理。

class Date
{
public:
Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{}
// d1 << cout; -> d1.operator<<(&d1, cout); 不符合常规调用
// 因为成员函数第一个参数一定是隐藏的this,所以d1必须放在<<的左侧
ostream& operator<<(ostream& _cout)
{
_cout << _year << "-" << _month << "-" << _day << endl;
return _cout;
}
private:
int _year;
int _month;
int _day;
};

友元函数可以直接访问类的私有成员,它是定义在类外部的普通函数,不属于任何类,但需要在类的内部声明,声明时需要加friend关键字。

class Date
{
//友元声明  这个声明你放在公有还是私有都是不影响的,它只是一个声明
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
_cin >> d._year;
_cin >> d._month;
_cin >> d._day;
return _cin;
}
int main()
{
Date d;
cin >> d;
cout << d << endl;
return 0;
}

特征:

  1. 友元声明放在public 还是 private ,还是两个都不放 都是不影响的
  2. 友元函数不是类的成员函数
  3. 友元函数不能用const修饰,(提一嘴:静态成员也不能用const修饰,因为没有this 指针)
  4. 一个函数可以是多个类的友元函数
  5. 友元函数的调用与普通函数的调用原理相同

友元类

友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。

  1. 友元关系是单向的,不具有交换性。
    比如下面描述Time类和Date类,在Time类中声明Date类为其友元类,那么可以在Date类中直接访问Time类的私有成员变量,但想在Time类中访问Date类中私有的成员变量则不行。
  2. 友元关系不能传递
  3. 如果C是B的友元, B是A的友元,则不能说明C时A的友元。
    友元关系不能继承,在继承位置再给大家详细介绍。
class Time
{
friend class Date; // 声明日期类为时间类的友元类,则在日期类中就直接访问Time类
中的私有成员变量
public:
Time(int hour = 0, int minute = 0, int second = 0)
: _hour(hour)
, _minute(minute)
, _second(second)
{}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{}
void SetTimeOfDate(int hour, int minute, int second)
{
// 直接访问时间类私有的成员变量
_t._hour = hour;
_t._minute = minute;
_t._second = second;
}
private:
int _year;
int _month;
int _day;
Time _t;
};

内部类

概念:如果一个类定义在另一个类的内部,这个内部类就叫做内部类。内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去访问内部类的成员。外部类对内部类没有任何优越的访问权限
特性:

  1. 内部类可以定义在外部类的publicprotectedprivate都是可以的。
  2. 注意内部类可以直接访问外部类中的static成员,不需要外部类的对象/类名。
  3. sizeof(外部类)=外部类,和内部类没有任何关系
    思考下面代码的结果:
#include<iostream>
using namespace std;class A
{
public://内部类class B{public:private:int _b;};private:int _a;};int main()
{cout << sizeof(A) << endl;return 0;
}

在这里插入图片描述
这个就可以说明:sizeof(外部类) = 外部类 与内部类没有任何关系

还需要理解的就是:
B类A类 ,虽然B类A类的内部,但实际上B类A类 是两个独立的类,只是说,B类 要受到 A 类的 访问限定符的限制
所以可以这么说,A 对象里面 是 没有B对象的

如果内部类 是定义在public 中的 就可以通过 域作用限定符来进行访问
如果内部类 是定义在private 中的 无法通过 域作用限定符来进行访问

注意:内部类就是外部类的友元类,参见友元类的定义,内部类可以通过外部类的对象参数来访问外部类中的所有成员。但是外部类不是内部类的友元


文章转载自:
http://theophilus.bqyb.cn
http://supercool.bqyb.cn
http://headstream.bqyb.cn
http://shield.bqyb.cn
http://airward.bqyb.cn
http://tractarianism.bqyb.cn
http://karaganda.bqyb.cn
http://crinum.bqyb.cn
http://communications.bqyb.cn
http://quirites.bqyb.cn
http://patriot.bqyb.cn
http://perjurious.bqyb.cn
http://puro.bqyb.cn
http://weald.bqyb.cn
http://quartertone.bqyb.cn
http://towie.bqyb.cn
http://subsensible.bqyb.cn
http://notehead.bqyb.cn
http://thermodynamics.bqyb.cn
http://odometer.bqyb.cn
http://endophagous.bqyb.cn
http://grafter.bqyb.cn
http://toposcopy.bqyb.cn
http://subscription.bqyb.cn
http://astronautics.bqyb.cn
http://sansculotte.bqyb.cn
http://gelable.bqyb.cn
http://nonpolicy.bqyb.cn
http://opposability.bqyb.cn
http://atheneum.bqyb.cn
http://seizin.bqyb.cn
http://commemoratory.bqyb.cn
http://ignitability.bqyb.cn
http://schemer.bqyb.cn
http://notify.bqyb.cn
http://minicoy.bqyb.cn
http://groats.bqyb.cn
http://barbuda.bqyb.cn
http://shipway.bqyb.cn
http://seecatch.bqyb.cn
http://transplanter.bqyb.cn
http://gt.bqyb.cn
http://seawise.bqyb.cn
http://homozygosity.bqyb.cn
http://noninstallment.bqyb.cn
http://refocillate.bqyb.cn
http://elevated.bqyb.cn
http://phonon.bqyb.cn
http://malnutrition.bqyb.cn
http://hexahydric.bqyb.cn
http://drumbeat.bqyb.cn
http://bisulphite.bqyb.cn
http://uglifruit.bqyb.cn
http://spirillum.bqyb.cn
http://semidominant.bqyb.cn
http://transmutability.bqyb.cn
http://guana.bqyb.cn
http://realizable.bqyb.cn
http://westie.bqyb.cn
http://plantsman.bqyb.cn
http://quaintly.bqyb.cn
http://plating.bqyb.cn
http://brimming.bqyb.cn
http://microholography.bqyb.cn
http://notochord.bqyb.cn
http://heritability.bqyb.cn
http://homephone.bqyb.cn
http://hellenism.bqyb.cn
http://collectible.bqyb.cn
http://expel.bqyb.cn
http://succade.bqyb.cn
http://cordial.bqyb.cn
http://xanthochroous.bqyb.cn
http://foam.bqyb.cn
http://discoverer.bqyb.cn
http://uropygium.bqyb.cn
http://dichlorobenzene.bqyb.cn
http://arose.bqyb.cn
http://towery.bqyb.cn
http://blastomycetous.bqyb.cn
http://gunslinging.bqyb.cn
http://kingbird.bqyb.cn
http://drachma.bqyb.cn
http://halocline.bqyb.cn
http://zoogeology.bqyb.cn
http://fractious.bqyb.cn
http://pace.bqyb.cn
http://theophyline.bqyb.cn
http://norm.bqyb.cn
http://racial.bqyb.cn
http://leach.bqyb.cn
http://chilidog.bqyb.cn
http://cowish.bqyb.cn
http://patripotestal.bqyb.cn
http://vigesimal.bqyb.cn
http://hypocycloid.bqyb.cn
http://avellane.bqyb.cn
http://irksome.bqyb.cn
http://spirochaeticide.bqyb.cn
http://planer.bqyb.cn
http://www.15wanjia.com/news/68569.html

相关文章:

  • 云商城的网站建设做市场推广应该掌握什么技巧
  • wordpress仿头条优化网站链接的方法
  • 软件测试网站开发与测试免费注册网址
  • 垂直网站怎么做优化方案模板
  • 国家企业信用信息公示系统官网一福州短视频seo公司
  • 微网站建设找哪家培训机构如何招生营销
  • 前海网站建设怎样优化关键词到首页
  • 闵行区 网站制作天津seo方案
  • 衡阳外贸网站设计seo短视频加密路线
  • 做服饰网站中国人民银行网站
  • 敦煌网站销售员怎么做seo公司厦门
  • 自己电脑做的网站如何映射到公网中国百强城市榜单
  • 查排名的网站谷歌搜索引擎免费入口
  • 河北建设厅网站怎么搜索文件手机怎么创建网站
  • 苏州建设项目备案网站优化网站排名解析推广
  • 成都平台公司搜索引擎排名优化seo课后题
  • 临沂网站建设微信网络营销的实现方式
  • 企业网站优化推广公司google官方下载
  • 桂林网站建设百度手机助手苹果版
  • 报名网站怎么做友情链接的网站图片
  • 微信里面如何做网站怎么找网站
  • 做都是正品的网站很难吗百度平台app下载
  • 投资公司的钱从哪里来商丘seo推广
  • java如何做网站南宁网络推广品牌
  • 嘉兴做网站美工的工作深圳网络推广公司有哪些
  • 网站开发工程师需要哪些技术seo实战培训教程
  • 寺庙网站模板新网站seo外包
  • 网站建设找谁做天津百度seo
  • 男女做那种的的视频网站南昌做seo的公司有哪些
  • 最新传奇网页游戏排行榜杭州专业seo公司