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

河北廊坊建设银行网站贵州建设厅文件网站首页

河北廊坊建设银行网站,贵州建设厅文件网站首页,做网站义乌,德州哪里做网站友元 意义 程序中#xff0c;有些私有属性也想让类外特殊的一些函数或者类进行访问#xff0c;就要用到友元技术 关键字 friend 友元的三种实现 全局函数做友元 class Room{friend void test(Person p);//friend class test;public:string phone_number;private:string…友元 意义 程序中有些私有属性也想让类外特殊的一些函数或者类进行访问就要用到友元技术 关键字 friend 友元的三种实现 全局函数做友元 class Room{friend void test(Person p);//friend class test;public:string phone_number;private:string bedroom; }void test(Person p){cout p-bedroom endl;}类做友元 friend class test; 成员函数做友元 friend void GoodGay::visit();GoodGay类下的visit成员函数作为本类的好朋友可以访问私有成员 运算符重载 概念 对已有的运算符重新进行定义赋予其另一种功能以适应不同的数据类型主要是重载函数的函数名就是operator这是编译器起好的 加号 成员函数重载调用的本质Person p3 p1.operator(p2); class Person{public:Person operator(Person p){Person temp;temp.m_A this-m_A p.m_A;temp.m_B this-m_B p.m_Areturn temp;}}全局函数重载调用的本质Person p3 operator(p1, p2); Person operator(Person p1, Person p2){Person temp;temp.m_A p1.m_A p2.m_A;temp.m_B p1.m_B p2.m_B;return temp;}函数重载依然适用于符号重载 左移—输出—可以输出自定义数据类型—需要配合友元 成员函数—不可行—因为无法实现对象在右边这种情况 p.operator(cout)简化为p cout 只能利用全局函数重载 operator(cout, p) 简化 cout p ostream operator(ostream cout, Person p){cout m_A p.m_A;return cout;}//cout其实也是一个对象为了满足链式编程故而不可以重新创建后返回因此采用引用问题—如果是私有属性这种全局函数无法访问怎么办—友元函数 递增/递减运算符重载—存在前置递增和后置递增 前置递增和后置递增的区分—采用占位参数前置递增返回的是引用后置递增返回的是数值 因为后置递增返回的是临时对象 class Num{friend ostream operator(ostream cout, Num n);private:int n_Num;public:Num(int a): n_Num(a) {return ;}Num operator(void){n_Num;return *this;}Num operator(int){Num temp (n_Num);n_Num;return temp;} };ostream operator(ostream cout, Num n){cout n.n_Num endl;return cout;}int main(){Num n(1);cout n endl;cout n endl;cout n endl;return 0;}赋值运算符 赋值运算其实也是默认就有的—属性值拷贝编译器提供的赋值运算符的操作是一个浅拷贝的操作因此在涉及到内存问题时候需要进行深拷贝故而需要进行重载 有连续赋值行因此涉及到链式编程 class Age{friend ostream operator(ostream cout, Age n);private:int* m_age;public:Age(int age){m_age new int(age);}Age(const Age age){if(m_age ! NULL){delete m_age;m_age NULL;}m_age new int(*age.m_age);}Age operator(Age p){if(m_age ! NULL){delete m_age;m_age NULL;}m_age new int(*p.m_age);return *this;}~Age(){if(m_age ! NULL){delete m_age;m_age NULL;}}};ostream operator(ostream cout, Age n){cout *n.m_age endl;return cout;}int main(){Age a(10);Age b(20);Age c(30);cout a endl;cout b endl;cout c endl;a b c;cout a endl;return 0;}关系运算符 意义 让两个对象可以进行比较 class Person{public:int m_age;string m_name;Person(string m_name, int m_age){this-m_name m_name;this-m_age m_age;}bool operator(Person p){if(this-m_age p.m_age this-m_name p.m_name){return true;}return false;}bool operator!(Person p){if(this-m_age ! p.m_age || this-m_name p.m_name){return true;}return false;}};函数调用运算 ()也可以重载由于重载后使用的方式非常像函数的调用因此称为访函数访函数没有固定写法非常灵活 class Myprintf{public:void operator()(string test){cout test endl;}}Myprintf printf;printf(hello cpp);匿名函数 类型()Myprintf()(“hello cpp”);
http://www.dnsts.com.cn/news/203531.html

相关文章:

  • 网站已有备案了 现在换空间商还用备案么网站开发的心得体会
  • 专做老酒的网站做推广效果哪个网站好
  • 小说网站怎么做防采集房屋租赁系统网站开发
  • 天津企业网站建设哪家好芜湖app开发
  • 太仓高端网站制作中国十大电商公司
  • asp网站表格代码顺德电子画册网站建设
  • 做网站昆山小程序定制开发中心
  • 南京 外贸网站建设禁止拿我们的网站做宣传
  • 教育培训网页设计南宁网站seo外包
  • 深圳筑造建设有限公司网站国内优秀网页网站设计
  • 永久免费网站推荐江苏永坤建设有限公司网站
  • 专门做母婴的网站易语言网站建设
  • 个人做网站做什么样的话打折网站建设教程下载
  • 网站更新问题类似稿定设计的网站
  • 易迈互联网站建设怎么样河北邯郸有几个区县
  • 做商城网站服务器配置怎么选择徐州seo公司
  • 郑州做网站元辰什么是网站空间
  • 做影视网站存储视频会侵权吗大学生为什么不去中建
  • 做网站推广收入好吗权重较高网站
  • 企业网站建设总体构架外包小程序开发的价格
  • 淘宝上网站开发退款关键词优化一般收费价格
  • 在网站做博客长沙传媒公司招聘
  • 开发网站需要什么开发工具游戏源码论坛
  • 郑州网站制淘宝网官方网站网页版
  • 百度免费网站如何建设站长之家特效网站
  • 分享经济网站怎么建设自动发卡网站怎么做
  • 台北网站建设为农村建设网站报告
  • 网站建设公司盈利详情页设计方法
  • 网站备案成功创建网址链接
  • 织梦网站建设流程wordpress全自动赚钱