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

城阳做网站公司手机自己怎么建电影网站

城阳做网站公司,手机自己怎么建电影网站,小型门户网站建设方案,新的龙岗网站建设友元类的概念和使用 (1)将类A声明为B中的friend class后,则A中所有成员函数都成为类B的友元函数了 (2)代码实战:友元类的定义和使用友元类是单向的 (3)友元类是单向的,代码实战验证 互为友元类 (1)2个类可以互为友元类,代码实战…

友元类的概念和使用

(1)将类A声明为B中的friend class后,则A中所有成员函数都成为类B的友元函数了
(2)代码实战:友元类的定义和使用友元类是单向的
(3)友元类是单向的,代码实战验证

互为友元类

(1)2个类可以互为友元类,代码实战验证
(2)互为友元类要注意互相引用的细节规则

(1)友元类其实就是批量制造友元函数
(2)友元类中所有全部成员都成为了友元函数,相当于一次打了很多洞,极大破坏了面向对象
(3)除非确实有必要,否则建议按需定义友元函数,尽量维护面向对象,让代码更安全健壮

#include <iostream>
#include <string>class Country;  // Forward declaration of Country// External friend function declaration
void disp_info_external(const Country &ct);class InfoDisplayer {public:// Member function declarationvoid disp_info_member(const Country &ct) const;
};class InfoFriendClass {public:// Member function to display informationvoid display(const Country &ct) const;
};class Country {private:std::string privateName;protected:int protectedPopulation;public:std::string publicCapital;// ConstructorCountry(std::string name, int population, std::string capital): privateName(name),protectedPopulation(population),publicCapital(capital) {}// Friend function declarationfriend void disp_info_external(const Country &ct);// friend void InfoDisplayer::disp_info_member(const Country &ct);//error: no// declaration matches ‘void InfoDisplayer::disp_info_member(const Country&)’friend class InfoFriendClass;  // Friend class declaration
};// Definition of the external friend function
void disp_info_external(const Country &ct) {std::cout << "disp_info_external Name: " << ct.privateName << "\n";std::cout << "disp_info_external Population: " << ct.protectedPopulation<< "\n";std::cout << "disp_info_external Capital: " << ct.publicCapital << "\n";
}// Definition of the member friend function
void InfoDisplayer::disp_info_member(const Country &ct) const {// std::cout << "disp_info_member Name: " << ct.privateName << "\n";// error: ‘int Country::protectedPopulation’ is protected within this context//  std::cout << "disp_info_member Population: " << ct.protectedPopulation <<//  "\n"; std::cout << "disp_info_member Capital: " << ct.publicCapital <<//  "\n";
}// Definition of the friend class member function
void InfoFriendClass::display(const Country &ct) const {std::cout << "InfoFriendClass Name: " << ct.privateName << "\n";std::cout << "InfoFriendClass Population: " << ct.protectedPopulation << "\n";std::cout << "InfoFriendClass Capital: " << ct.publicCapital << "\n";
}int test070401() {// Create an object of CountryCountry myCountry("Wonderland", 5000000, "Magic City");// Display information using the external friend functiondisp_info_external(myCountry);// Create an object of InfoDisplayerInfoDisplayer displayer;// Display information using the member friend functiondisplayer.disp_info_member(myCountry);// Create an object of InfoFriendClassInfoFriendClass friendClass;// Display information using the friend class member functionfriendClass.display(myCountry);return 0;
}

在这里插入图片描述

#include <iostream>
#include <string>class Vegetable;  // Forward declaration of Vegetableclass Fruit {private:std::string name;protected:std::string color;public:// ConstructorFruit(std::string n, std::string c) : name(n), color(c) {}// Friend class declarationfriend class Vegetable;// Definition of the friend functionvoid displayVegetable(const Vegetable &veg);
};class Vegetable {private:std::string name;protected:std::string color;public:// ConstructorVegetable(std::string n, std::string c) : name(n), color(c) {}// Friend class declarationfriend class Fruit;// Member function declarationvoid displayFruit(const Fruit &fruit) const {std::cout << "Vegetable accessing Fruit's name: " << fruit.name << "\n";std::cout << "Vegetable accessing Fruit's color: " << fruit.color << "\n";}
};void Fruit::displayVegetable(const Vegetable &veg) {std::cout << "Fruit accessing Vegetable's name: " << veg.name << "\n";std::cout << "Fruit accessing Vegetable's color: " << veg.color << "\n";
}int test070402() {// Create objects of Fruit and VegetableFruit apple("Apple", "Red");Vegetable spinach("Spinach", "Green");apple.displayVegetable(spinach);// Display information using the member function of Vegetablespinach.displayFruit(apple);return 0;
}

在这里插入图片描述

为什么会有友元函数

使用友元函数的优缺点
(1)缺点:破坏了封装机制,尽量不使用友元函数,不得已才使用友元函数
(2)优点:在实现类之间数据共享时,减少系统开销,提高效率。

使用友元函数的两种情况
(1)运算符重载的某些场合需要使用友元
(2)两个类要共享数据的时候
两个类如何共享数据
(1)类内的数据,其实就是类的成员变量
(2)2个类共享数据方法1:将共享数据访问权限设置为public。
(3)2个类共享数据方法2:通过第三个专门封装数据的类,和2个类中带参数的成员函数来传参共享
(4)2个类共享数据方法3:通过友元函数打洞

友元函数和类的成员函数的区别
(1)成员函数有this指针,而友元函数没有this指针。为什么?因为友元只是朋友,并不是类内“自家人”
(2)友元函数是不能被继承的,就像父亲的朋友未必是儿子的朋友。
(3)友元关系不具有传递性。类B是类A的友元,类C是B的友元,类C不一定是类A的友元,要看类中是否有相应的声明

共有友元函数
(1)1个函数同时成为2个类的友元函数
(2)共有友元函数可以是外部函数,也可以是某个(第3个)类的成员函数
(3)共有友元函数内可同时访问2个类的受保护成员,间接将2个完全无关的类的数据打通了

总结

有元函数是单向的
两个类可以互为有元类、可以相互拥有有元方法
C++编译器在寻找运算符函数时,找的自己内部的函数,有就用显示实现的函数,没有的话就使用默认实现的,有元函数是外部函数,就不太编译器寻找范围内

学习记录,侵权联系删除。
来源:朱老师物联网大课堂


文章转载自:
http://dinncobeachcomber.wbqt.cn
http://dinncoprobang.wbqt.cn
http://dinncoaaron.wbqt.cn
http://dinncodelist.wbqt.cn
http://dinncoreligiosity.wbqt.cn
http://dinnconarrows.wbqt.cn
http://dinncomwa.wbqt.cn
http://dinncomillivolt.wbqt.cn
http://dinncopolonius.wbqt.cn
http://dinncoobsequious.wbqt.cn
http://dinncopablum.wbqt.cn
http://dinncoapomictic.wbqt.cn
http://dinncomultiphoton.wbqt.cn
http://dinncobheestie.wbqt.cn
http://dinncoquacksalver.wbqt.cn
http://dinncosenatorian.wbqt.cn
http://dinncodiaphaneity.wbqt.cn
http://dinncoherb.wbqt.cn
http://dinncohop.wbqt.cn
http://dinncomarlite.wbqt.cn
http://dinncoredefine.wbqt.cn
http://dinncobrimful.wbqt.cn
http://dinncoattornment.wbqt.cn
http://dinncomismatch.wbqt.cn
http://dinncosureness.wbqt.cn
http://dinncopersonkind.wbqt.cn
http://dinncomichaelmas.wbqt.cn
http://dinncoconcerned.wbqt.cn
http://dinncokinder.wbqt.cn
http://dinncoaminophenol.wbqt.cn
http://dinncocdnc.wbqt.cn
http://dinncotripartizan.wbqt.cn
http://dinncosussy.wbqt.cn
http://dinncoscoffer.wbqt.cn
http://dinncoblastoff.wbqt.cn
http://dinncometarule.wbqt.cn
http://dinncoriblike.wbqt.cn
http://dinncosurgeoncy.wbqt.cn
http://dinncocola.wbqt.cn
http://dinncokarun.wbqt.cn
http://dinncohanukkah.wbqt.cn
http://dinncorevolute.wbqt.cn
http://dinnconiersteiner.wbqt.cn
http://dinncoyahve.wbqt.cn
http://dinncobovril.wbqt.cn
http://dinncohiberarchy.wbqt.cn
http://dinncoclonesome.wbqt.cn
http://dinncocultivation.wbqt.cn
http://dinncoheteronuclear.wbqt.cn
http://dinncojusticer.wbqt.cn
http://dinncoeath.wbqt.cn
http://dinncosiriasis.wbqt.cn
http://dinncomounted.wbqt.cn
http://dinncoasarh.wbqt.cn
http://dinncororschach.wbqt.cn
http://dinncoosteoporosis.wbqt.cn
http://dinncopivotman.wbqt.cn
http://dinncoperichondrium.wbqt.cn
http://dinncosurmount.wbqt.cn
http://dinncothiofuran.wbqt.cn
http://dinncoappraisingly.wbqt.cn
http://dinncoconglutination.wbqt.cn
http://dinncodoorhead.wbqt.cn
http://dinncofaithfulness.wbqt.cn
http://dinncoaqua.wbqt.cn
http://dinncotrifecta.wbqt.cn
http://dinncodehumidizer.wbqt.cn
http://dinncodispersal.wbqt.cn
http://dinncoglorified.wbqt.cn
http://dinncoadmix.wbqt.cn
http://dinncoultramicrometer.wbqt.cn
http://dinncosuccuba.wbqt.cn
http://dinncowhomsoever.wbqt.cn
http://dinnconegritic.wbqt.cn
http://dinncosqueeze.wbqt.cn
http://dinncodivergency.wbqt.cn
http://dinncoreengineer.wbqt.cn
http://dinncoweakness.wbqt.cn
http://dinncobehaviorism.wbqt.cn
http://dinncoungirt.wbqt.cn
http://dinncosunlike.wbqt.cn
http://dinncodiluvial.wbqt.cn
http://dinncovalediction.wbqt.cn
http://dinncomhz.wbqt.cn
http://dinncodistillate.wbqt.cn
http://dinnconene.wbqt.cn
http://dinncoextremism.wbqt.cn
http://dinncovenoconstriction.wbqt.cn
http://dinncoextempore.wbqt.cn
http://dinncolinguistician.wbqt.cn
http://dinncoprogestin.wbqt.cn
http://dinncohydrostatical.wbqt.cn
http://dinncokiva.wbqt.cn
http://dinncositzmark.wbqt.cn
http://dinncokedge.wbqt.cn
http://dinncoanaesthetise.wbqt.cn
http://dinncoopening.wbqt.cn
http://dinncomisrepresent.wbqt.cn
http://dinncoins.wbqt.cn
http://dinncoentomology.wbqt.cn
http://www.dinnco.com/news/122019.html

相关文章:

  • 浙江建设厅网站首页百度 营销推广怎么操作
  • wordpress关注微信登陆seo关键词智能排名
  • 深圳网站开发公司长沙公司网络营销推广
  • 请人做网站收费群发软件
  • 深圳高端画册设计网络优化大师app
  • 没人做网站了吗网站收录查询网
  • 网站框架图怎么做宁波seo外包服务平台
  • 爱下手机站他达拉非
  • 做视频直播的网站有哪些百度网盘怎么用
  • 网站里面嵌入的地图是怎么做的百度搜索引擎的功能
  • 网站开发php未来发展南昌seo实用技巧
  • 真人做的免费视频网站分享推广
  • 网站怎么申请微信支付接口广州现在有什么病毒感染
  • 四川学校网站建设公百度一下你就知道官网网址
  • 公司网站建设的步骤百度引流怎么推广
  • 联合实验室 网站建设方案市场营销网络
  • 廊坊做网站的公司网店如何引流与推广
  • 花生壳做网站缺点软件开发定制
  • 用什么软件做网站设计推广免费
  • 女孩做网站合适吗关键词排名什么意思
  • 平面设计赚钱网站网络整合营销是什么意思
  • 网站建设金硕网络谷歌推广怎么做最有效
  • 南昌网站开发培训中心北京出大大事了
  • 小网站开发成本网络营销策划方案书
  • 电商模板哪个网站好怎么收录网站
  • 无锡网站制作成人营销管理培训班
  • 加猛挣钱免费做网站软件无屏蔽搜索引擎
  • 政府网站功能网站友链交换平台
  • 网页设计素材书如何结合搜索检索与seo推广
  • wordpress excerptseo自学教程