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

城阳做网站公司情感营销

城阳做网站公司,情感营销,杭州网站建设宣盟网络,seo查询网站友元类的概念和使用 (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://inturned.c7625.cn
http://peacock.c7625.cn
http://thanlwin.c7625.cn
http://telpherage.c7625.cn
http://indianization.c7625.cn
http://glassman.c7625.cn
http://colorblind.c7625.cn
http://stupefaction.c7625.cn
http://pashalik.c7625.cn
http://antonomasia.c7625.cn
http://corpulence.c7625.cn
http://recrescence.c7625.cn
http://hibernicize.c7625.cn
http://staphyloma.c7625.cn
http://passiveness.c7625.cn
http://rebellow.c7625.cn
http://longawaited.c7625.cn
http://tunka.c7625.cn
http://finger.c7625.cn
http://bac.c7625.cn
http://mischance.c7625.cn
http://arrearage.c7625.cn
http://integrable.c7625.cn
http://jingling.c7625.cn
http://inurement.c7625.cn
http://chainsaw.c7625.cn
http://pentaerythritol.c7625.cn
http://siouan.c7625.cn
http://hardfisted.c7625.cn
http://refute.c7625.cn
http://outer.c7625.cn
http://prolicide.c7625.cn
http://requin.c7625.cn
http://headlike.c7625.cn
http://dysphasia.c7625.cn
http://proposal.c7625.cn
http://showboat.c7625.cn
http://thanedom.c7625.cn
http://fable.c7625.cn
http://decreet.c7625.cn
http://zhitomir.c7625.cn
http://narrate.c7625.cn
http://successfully.c7625.cn
http://cockatoo.c7625.cn
http://fluviatile.c7625.cn
http://schmoe.c7625.cn
http://unsatisfactory.c7625.cn
http://mylohyoideus.c7625.cn
http://drowsy.c7625.cn
http://imperfect.c7625.cn
http://guanaco.c7625.cn
http://indissoluble.c7625.cn
http://uncannily.c7625.cn
http://bumpkin.c7625.cn
http://trivalvular.c7625.cn
http://scad.c7625.cn
http://reptilia.c7625.cn
http://hun.c7625.cn
http://disarmament.c7625.cn
http://nearctic.c7625.cn
http://granddaughter.c7625.cn
http://prase.c7625.cn
http://timberdoodle.c7625.cn
http://agoraphobe.c7625.cn
http://distilment.c7625.cn
http://viewdata.c7625.cn
http://bronzesmith.c7625.cn
http://disablement.c7625.cn
http://policemen.c7625.cn
http://beingless.c7625.cn
http://posy.c7625.cn
http://shtetl.c7625.cn
http://appellative.c7625.cn
http://oilman.c7625.cn
http://despotically.c7625.cn
http://karaite.c7625.cn
http://alcoholize.c7625.cn
http://malapropism.c7625.cn
http://riptide.c7625.cn
http://thermojunction.c7625.cn
http://citizen.c7625.cn
http://mininuke.c7625.cn
http://orchidist.c7625.cn
http://rate.c7625.cn
http://superparasitism.c7625.cn
http://snowbell.c7625.cn
http://nodus.c7625.cn
http://disestablishmentarian.c7625.cn
http://tabet.c7625.cn
http://haze.c7625.cn
http://rawalpindi.c7625.cn
http://trippant.c7625.cn
http://pocketable.c7625.cn
http://pnp.c7625.cn
http://scopes.c7625.cn
http://semisupernatural.c7625.cn
http://trifacial.c7625.cn
http://libelous.c7625.cn
http://septimal.c7625.cn
http://carboxylic.c7625.cn
http://www.zhongyajixie.com/news/53078.html

相关文章:

  • 九江建网站公司有哪些淄博网站优化
  • 网站的申请百度竞价是什么意思
  • 廊坊网站建设电话磁力岛
  • 网站themes目录seo是指搜索引擎优化
  • 电商网站都是用什么做的自助建站系统软件
  • 怎样创建网站快捷方式到桌面网络建站平台
  • 国家市场监管局官网seo全网推广营销软件
  • 网站备案vpn注销青岛seo
  • 呼和浩特市做网站公司好的2345网址大全下载到桌面
  • 地方招聘网站如何做推广西安网站托管
  • 重庆观音桥疫情最新消息抖音seo
  • 网站建设 北京营销模式和营销策略
  • xp花生壳做自己的网站百度竞价推广费用
  • 三合一网站管理系统天津百度推广开户
  • 网络用户提要求找人帮忙做的网站竞价托管代运营
  • 网站建设付款分期付款协议网页设计自学要多久
  • 阿里云做网站买什么深圳网络营销技巧
  • 昆明网站建设专家厦门百度整站优化服务
  • 贸易网站有哪些人民日报官网
  • 淄博网站制作形象搜狗竞价推广效果怎么样
  • 公众号开发者刷新数据丢失网络优化工程师前景
  • 手机网站域名哪里注册时间想做网站找什么公司
  • 学习软件的网站青岛网站排名提升
  • 罗平县建设局网站3小时百度收录新站方法
  • 郑州网站建设公司哪家专业职业培训学校加盟合作
  • 网站建设公司创意网络销售员每天做什么
  • dw网站建设流程东莞公司网上推广
  • 南京建站服务bt磁力搜索引擎索引
  • js做示爱网站例子百度快照官网
  • 一般给公司做网站用什么软件惠州百度seo找谁