泰安网站建设哪里有网站建设 百度云盘
- 作者: 五速梦信息网
- 时间: 2026年03月21日 08:22
当前位置: 首页 > news >正文
泰安网站建设哪里有,网站建设 百度云盘,昆明市住房和城乡建设局官方网站,没有收款接口网站怎么做收款本篇文章来介绍一下C中的运算符重载#xff0c;以及与运算符重载有关的三个默认默认成员函数#xff1a;赋值运算符重载#xff0c;普通对象取地址与const对象取地址操作符重载#xff0c;也就是下面图片中6个默认成员函数的后三个#xff0c;前三个默认成员函数在之前文章… 本篇文章来介绍一下C中的运算符重载以及与运算符重载有关的三个默认默认成员函数赋值运算符重载普通对象取地址与const对象取地址操作符重载也就是下面图片中6个默认成员函数的后三个前三个默认成员函数在之前文章中已经讲过类和对象构造函数析构函数与拷贝构造函数_一棵西兰花的博客-CSDN博客https://blog.csdn.net/qq_72916130/article/details/132789343?spm1001.2014.3001.5501大家不太清楚的可以去看一下。 本篇文章主要通过一个日期类来进行讲解。我会把完整的日期类放到后面。 class Date { public://… private:int _year;int _month;int _day; };1.什么是运算符重载 C为了增强代码的可读性引入了运算符重载运算符重载是具有特殊函数名的函数也具有其返回值类型函数名字以及参数列表其返回值类型与参数列表与普通的函数类似。 函数名字为关键字operator后面接需要重载的运算符符号。 函数原型返回值类型 operator操作符(参数列表) 注意 不能通过连接其他符号来创建新的操作符比如operator重载操作符必须有一个类类型参数用于内置类型的运算符其含义不能改变例如内置的整型不 能改变其含义作为类成员函数重载时其形参看起来比操作数数目少1因为成员函数的第一个参数为隐藏的this指针.* :: sizeof ? : . 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。 光看定义是不行的我们实现以下 和 的重载 // 运算符重载 //因为号有两个操作数因为有一个是隐藏的this指针所以这里只有一个参数 bool Date::operator(const Date d) {if (_year d._year){return false;}else if (_year d._year _month d._month){return false;}else if (_year d._year _month d._month _day d._day){return false;}else{return true;} }// 运算符重载 bool Date::operator(const Date d) {return _year d._year _month d._month _day d._day; }有一个 和 或 和 其他比较运算符就可以有这两个推导出来不用再一个一个写。 2.赋值运算符重载 1.赋值运算符重载格式 参数类型const T传递引用可以提高传参效率加const是为了防止参数被修改返回值类型T返回引用可以提高返回的效率有返回值目的是为了支持连续赋值检测是否自己给自己赋值返回*this 要符合连续赋值的含义 代码 Date Date::operator(const Date d)//可以不用引用但没有必要 {if (this ! d)//自己给自己赋值{_year d._year;_month d._month;_day d._day;}return *this; }对于返回*this因为我们平时的赋值运算符可以实现 类似与 a b c d 的方式显示调用就是a.operator(b.operator(c.operator(d)))如果operator函数没有返回值就会出现错误。 2.赋值运算符只能重载成类的成员函数不能重载成全局函数 // 赋值运算符重载成全局函数注意重载成全局函数时没有this指针了需要给两个参数 Date operator(Date left, const Date right) {if (left ! right){left._year right._year;left._month right._month;left._day right._day;}return left; }// 编译失败 // error C2801: “operator ”必须是非静态成员 原因赋值运算符如果不显式实现编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载就和编译器在类中生成的默认赋值运算符重载冲突了故赋值运算符重载只能是类的成员函数。 3.用户没有显式实现时编译器会生成一个默认赋值运算符重载以值的方式逐字节拷贝。注意内置类型成员变量是直接赋值的而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。 既然编译器生成的默认赋值运算符重载函数已经可以完成字节序的值拷贝了还需要自己实 现吗当然像日期类这样的类是没必要的。 注意如果类中未涉及到资源管理赋值运算符是否实现都可以一旦涉及到资源管理则必须要实现。因为他会将地址也进行简单的值拷贝(浅拷贝)在进行析构时回释放两次会导致程序崩溃。 3.前置与后置重载 前置与后置的操作数只有一个而且只有返回值不同一个前置返回自己加一后置需要在构造一个Date 来存放没有加一时的结果进行返回。只有返回值不同没有办法区分两个函数只好在后置加入一个占位参数没有什么用途只是为了区分前置与后置编译器也是通过这个方式来识别。 C规定后置重载时多增加一个int类型的参数但调用函数时该参数不用传递编译器自动传递 代码 // 前置 Date Date::operator() {*this 1;return *this; }// 注意后置是先使用后1因此需要返回1之前的旧值 //故需在实现时需要先将this保存一份然后给this1 // 而temp是临时对象因此只能以值的方式返回不能返回引用 // 后置 传入的值不用接收 Date Date::operator(int) {Date tmp(*this);*this 1;return tmp; }– 也一样 // 后置– Date Date::operator–(int) {Date tmp(*this);*this - 1;return tmp; } // 前置– Date Date::operator–() {*this - 1;return this; }4.const成员 将const修饰的“成员函数”称之为const成员函数const修饰类成员函数,这里的const需要加在形参列表的后面实际修饰该成员函数隐含的this指针表明在该成员函数中不能对类的任何成员进行修改。 我们来看看下面的代码 这里类成员函数Print() 没有用const修饰。 const Date d1(2023, 9, 23);//const修饰的对象调用不了成员函数因为权限放大d1.Print();//d1.print(d1) 隐藏的传入this的指针是const Data 类型 Print() 没有被const 修饰函数形参this指针是 普通的 Date* 类型而被const修饰的对象调用Print()函数传入的this指针是const Data* 类型 会发生权限的放大权限可以缩小平移但是不能放大会报错。 请思考下面的几个问题 const对象可以调用非const成员函数吗非const对象可以调用const成员函数吗const成员函数内可以调用其它的非const成员函数吗非const成员函数内可以调用其它的const成员函数吗 答案13不可以权限放大。24可以权限缩小。 我们可以通过函数重载使不同权限的对象调用不同的函数。如下面代码的Print class Date { public:Date(int year, int month, int day){_year year;_month month;_day day;}void Print(){cout Print() endl;cout year: _year endl;cout month: _month endl;cout day: _day endl endl;}void Print() const{cout Print()const endl;cout year: _year endl;cout month: _month endl;cout day: _day endl endl;} private:int _year; // 年int _month; // 月int _day; // 日 }; void Test() {Date d1(2022,1,13);d1.Print();const Date d2(2022,1,13);d2.Print(); } 当然Print函数用const 修饰是没有什么意义的。我们看下面的取地址及const取地址操作符重载。 5.取地址及const取地址操作符重载 这两个默认成员函数一般不用重新定义 编译器默认会生成 class Date { public ://这种方式返回指针的内容可以修改可读可写Date* operator(){return this ;}//这种方式返回指针的内容也不能修改只读const Date* operator()const{return this ;} private :int _year ; // 年int _month ; // 月int _day ; // 日 }; 这两个运算符一般不需要重载使用编译器生成的默认取地址的重载即可只有特殊情况才需要重载比如想让别人获取到指定的内容不返回this返回nullptr等等。
- Date 类的实现 Date.h class Date {public:// 获取某年某月的天数int GetMonthDay(int year, int month);//全缺省的构造函数Date(int year 1900, int month 1, int day 1);// 赋值运算符重载// d2 d3 - d2.operator(d2, d3)Date operator(const Date d);// 日期天数Date operator(int day);// 日期天数Date operator(int day);// 日期-天数Date operator-(int day);// 日期-天数Date operator-(int day);void Print();//下面两个是运算符重载也构成函数重载// 前置 //d1 - d1.operator()Date operator();// 后置//d1 - d1.operator(0)//传一个整形即可只是用来区分前置//加了一个int参数进行占位跟前置构成函数重载进行区分//本质后置调用编译器进行了特殊处理Date operator(int);//传入的值不用接收// 后置–Date operator–(int);// 前置–Date operator–();// 运算符重载bool operator(const Date d);// 运算符重载bool operator(const Date d);// 运算符重载bool operator (const Date d);// 运算符重载bool operator (const Date d);// 运算符重载bool operator (const Date d);// !运算符重载bool operator ! (const Date d);// 日期-日期 返回天数int operator-(const Date d);const Date* operator()const;Date* operator();private:int _year;int _month;int _day; }; Date.cpp // 获取某年某月的天数 int Date::GetMonthDay(int year, int month) {static int MonthDay[13] { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month 2 (year % 4 0 year % 100 ! 0 || year % 400 0)){return 29;}return MonthDay[month]; }//全缺省的构造函数 //声明和定义缺省参数的值不能都给,必须在声明中给 //因为在编译时头文件中看到的是声明而声明中没有提供缺省值会报错。到不了后面的链接通过函数名修饰规则来找到函数的定义 Date::Date(int year, int month, int day) {_year year;_month month;_day day;//检查日期日期是否合法if (month 1 || month12 || day 1||_day GetMonthDay(_year, _month)){cout 非法日期\n endl;exit(-1);} }// 赋值运算符重载 // d2 d3 - d2.operator(d2, d3) Date Date::operator(const Date d)//可以不用引用但没有必要 {if (this ! d)//自己给自己赋值{_year d._year;_month d._month;_day d._day;}return *this; }// 日期天数 Date Date::operator(int day)//0次拷贝 0次赋值 {if (day 0){return *this - (-day);}_day day;while (_day GetMonthDay(_year, _month)){_day - GetMonthDay(_year, _month);_month;while (_month 12){_month 1;_year;}}return *this; } // 日期天数 Date Date::operator(int day)//两次拷贝 {if (day 0){return *this - (-day);}Date ret(*this);ret day;return ret; }// 日期-天数 Date Date::operator-(int day) {if (day 0){return *this (-day);}Date ret *this;ret - day;return ret; }// 日期-天数 Date Date::operator-(int day) {if (day 0){return *this (-day);}_day - day;while (_day 1){_month–;while (_month 1){_month 12;_year–;}_day GetMonthDay(_year, _month);}return this; }//只读函数可以加const,内部不涉及修改成员//void Date::Print(const Data this) //void Date::Print() //可以同时存在因为this指针类型不同如果只有const版本也可调用因为权限可以缩小 //{//不过在这里没有意义 // cout _year / _month / _day endl; //} void Date::Print() const {cout _year / _month / _day endl; }// 前置 Date Date::operator() {*this 1;return *this; }// 后置 Date Date::operator(int) {Date tmp(*this);*this 1;return tmp; } // 后置– Date Date::operator–(int) {Date tmp(*this);*this - 1;return tmp; } // 前置– Date Date::operator–() {*this - 1;return *this; }// 运算符重载 bool Date::operator(const Date d) {if (_year d._year){return false;}else if (_year d._year _month d._month){return false;}else if (_year d._year _month d._month _day d._day){return false;}else{return true;} }// 运算符重载 bool Date::operator(const Date d) {return _year d._year _month d._month _day d._day; }// 运算符重载 bool Date::operator (const Date d) {return *this d || *this d; }// 运算符重载 bool Date::operator (const Date d) {return !(*this d); }// 运算符重载 bool Date::operator (const Date d) {return !(*this d); }// !运算符重载 bool Date::operator ! (const Date d) {return !(*this d); }//跟日期-天数构成函数重载 // 日期-日期 返回天数 int Date::operator-(const Date d) {Date max *this;Date min d;int flag 1;if (max min){min *this;max d;flag -1;}int count 0;while (max ! min){–max;count;}return count * flag; }//日常自动生成的就可以 //不要被取到有效地址 //const Date* Date::operator()const //{ // return nullptr; //} const Date* Date::operator()const {return this; } //这个接口是读写 Date* Date::operator()//函数重载一个给const对象用一个给非const对象用 {return this; } 本篇结束
- 上一篇: 泰安网站建设广告长沙网络推广公司
- 下一篇: 泰安网站建设排行外贸网站优化推广
相关文章
-
泰安网站建设广告长沙网络推广公司
泰安网站建设广告长沙网络推广公司
- 技术栈
- 2026年03月21日
-
泰安网站建设方案书王野天与葛优
泰安网站建设方案书王野天与葛优
- 技术栈
- 2026年03月21日
-
泰安网络科技公司志鸿优化设计
泰安网络科技公司志鸿优化设计
- 技术栈
- 2026年03月21日
-
泰安网站建设排行外贸网站优化推广
泰安网站建设排行外贸网站优化推广
- 技术栈
- 2026年03月21日
-
泰安营销型手机网站建设亚马逊主机wordpress
泰安营销型手机网站建设亚马逊主机wordpress
- 技术栈
- 2026年03月21日
-
泰安整站优化手机建站平台
泰安整站优化手机建站平台
- 技术栈
- 2026年03月21日
