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

免费网站模板 下载浦东新区建设工程安全质量监督站网站

免费网站模板 下载,浦东新区建设工程安全质量监督站网站,网站建设与管理考试题,wordpress 324依然是温故而知新#xff0c;不过现在更多的是以此为乐的心态啦。本篇通过代码实例#xff0c;展示c函数重载相关知识#xff0c;包括构造函数的重载、操作符重载等。 在构造函数重载中#xff0c;给大家带来点稍微提升的用法#xff0c; 看了不吃亏#xff0c;看了不上当…依然是温故而知新不过现在更多的是以此为乐的心态啦。本篇通过代码实例展示c函数重载相关知识包括构造函数的重载、操作符重载等。 在构造函数重载中给大家带来点稍微提升的用法 看了不吃亏看了不上当 鞭策下面的代码吧以后在某些场景上SHOW一把时你可能用的上。 #include iostream #include assert.h #include sal.h #include string #include vectorusing namespace std; 知识点do{ statement; }while(0) 经常用在宏定义中减少一些错误的发生 // #ifdef MYTRACE() #else #define MYTRACE() do { std::cout __FUNCTION__ ,line __LINE__ std::endl;}while(0) #endifclass Student { public:/// constructStudent(){MYTRACE();}Student( _In_ const std::string name):m_name(name){MYTRACE();}Student(const std::string name,const int age):m_name(name),m_age(age){MYTRACE();}/// copy constructStudent( _In_ const Student that){this-m_age that.m_age;this-m_name that.m_name;MYTRACE();}/// 拷贝赋值Student operator(const Student that){if(this that){return *this;}this-m_age that.m_age;this-m_name that.m_name;return *this;}/// 操作符重载friend ostream operator (ostream out,const Student that){return out that.m_name is that.m_age years old.;}bool operator !(const Student that){return !(that.age() this-m_age that.name() this-m_name);}bool operator (const Student that){return that.age() this-m_age that.name() this-m_name;}/// 常成员函数const int age() const { return m_age;}const std::string name() const { return m_name;}private:int m_age 0;std::string m_name; };Student 中我们使用了C中的一些基础知识只能说中规中矩平平无奇。 class StudentList { public:/*********************construct*******************/StudentList(){}StudentList(std::vectorStudent l):m_sl(l){}StudentList(const Student t){ m_sl.push_back(t); }StudentList(const Student t){ m_sl.push_back(t); }/// 禁用拷贝构造和拷贝赋值StudentList(const StudentList that) delete;void operator (const StudentList that) delete;/// 玩点儿高级的参数列表构造void append(const Student t){ m_sl.push_back(t); }template typename ...Argsvoid append(const Student t, const Args... args) {m_sl.push_back(t);append(args...); // 递归}template typename ...ArgsStudentList(const Student t,const Args... args){ append(t,args...);}/// 流式构造StudentList operator(const Student t) { append(t); return *this; }/*********************construct*******************/const std::vectorStudent data() const{return m_sl;}/******************************* 操作符重载 ************************/friend ostream operator (ostream out,const StudentList that){for (const auto v : that.data()) { out ( v.name() , v.age() ) ; }return out;}const Student operator []( _In_ const unsigned index)const {assert(index m_sl.size());return m_sl[index];}const Student at(_In_ const unsigned index) const { return m_sl[index];}bool operator !(const StudentList that){if(this that){return false;}if(this-data().size() that.data().size()){for(unsigned i 0; i that.data().size();i){if( const_castStudent(this-data()[i])! const_castStudent(that.data()[i])){return true;}}}return true;}bool operator (const StudentList that){return !(const_castStudentList(that) ! const_castStudentList(*this));}/// 这里不做去重处理StudentList operator (const StudentList that){for(auto v : that.data()){append(v);}return *this;}/******************************* 操作符重载 ************************/private:std::vectorStudent m_sl; };StudentList 类中我们做了一些有意思的尝试譬如让StudentList支持参数列表的初始化,这让他看起来显然cool了一些其次我们通过重载 operator 操作符增加了支持流式的操作。这类似Qt中的 QStringList() wo miss u;是不是瞬间高大上了一些呢。有点像写文章时堆砌的华丽辞藻显然不是一些高级语法的应用其实只是为了让我们的程序更加优雅更健壮~ int main() {/// 测试重载的Student构造函数Student st1;Student st2(LiLei,25);Student st3(Keke,26);/// 测试拷贝构造函数Student st4(st3);std::cout st4 std::endl;st4 st2;std::cout st4 std::endl;/// list类型参数 构造函数StudentList sl(std::vectorStudent{st2,st3,st4});std::cout sl std::endl;/// 参数列表 递归构造函数StudentList sl1{st2,st3,st4,Student(zhanglu,24)};std::cout sl1 std::endl;/// 流式构造StudentList sl2;sl2 st2 st3 st4 Student(zhaoya,25) Student(lixiu,25);std::cout sl2 std::endl;// 测试使用下标重载函数std::cout sl2[1] std::endl;std::cout sl2.at(0) sl2.at(1) std::endl;// std::cout sl2[6] std::endl; // error// 测试 ! 和 操作符std::string sflag (sl2 ! sl2) ? true : false;std::cout sflag std::endl;sflag (sl2 sl2) ? true : false;std::cout sflag std::endl;sflag (sl2 ! sl1) ? true : false;std::cout sflag std::endl;// 测试 操作符StudentList sl3{Student(xiaoli,25)};sl3 sl2;std::cout sl3 std::endl;return 0; }ok有兴趣你也动手试试吧~ 每日几行代码 一瓶劲酒祝吾早日登升天
http://www.huolong8.cn/news/358453/

相关文章:

  • 广东网站建设建站模板wordpress还原回收站
  • 网站开发排期表模板深圳西乡房价多少一平方
  • 江西网站开发哪家好查询公司
  • wordpress站群服务器婚恋网站制作要多少钱
  • 东莞市电商网站建设摄影网站网址大全
  • 网站 排版模板阿里大数据分析平台
  • 中国十大黄页网站排行榜自己可以做门户网站吗
  • 电子产品东莞网站建设做班级网站代码
  • seo整站优化外包服务网站开发 定制 多少 钱
  • 免费做那个的视频网站好wordpress开启ssl强制跳转
  • 自助开通网站网页设计素材 旅游
  • 专业创建网站公司电子商务网站设计岗位主要是?
  • 网站建设业务员论坛可以用手机建设网站吗
  • 商城站到汤泉池网站域名价值查询
  • 智慧团建网站密码合肥网站建设电话咨询
  • 安徽芜湖网站建设网站开发专业培训学校
  • 如何做电影网站赚钱吗做网站需要具备的基础条件
  • 北京随喜设计网站一个下载网站
  • 去越南做网站微信开发者工具
  • 商丘seo教程seo网站有优化培训班吗
  • 南京机关建设网站wordpress5.0样式
  • 凡总创业网站设计之家官网效果图
  • 深圳几百元做网站wordpress 播放大视频
  • 济宁培训网站建设wordpress如何屏蔽特定国家的用户
  • 泰安市网站建设免费下载小程序
  • 什么是网站建设需求企业站模板大全
  • 电子商务网站建设与管理第二版答案什么公司做网站最好
  • 建设织梦网站模板做外贸英文网站
  • 公司名被注册网站网站产品介绍页面的布局方案
  • 怎么将自己做的网站上线郑州一建南阳分公司