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

郑州网站建设 天强科技杭州排名推广

郑州网站建设 天强科技,杭州排名推广,免费咨询律师软件,石家庄优化公司哪家好👦个人主页:Weraphael ✍🏻作者简介:目前学习C和算法 ✈️专栏:C航路 🐋 希望大家多多支持,咱一起进步!😁 如果文章对你有帮助的话 欢迎 评论💬 点赞&#x1…

在这里插入图片描述

👦个人主页:@Weraphael
✍🏻作者简介:目前学习C++和算法
✈️专栏:C++航路
🐋 希望大家多多支持,咱一起进步!😁
如果文章对你有帮助的话
欢迎 评论💬 点赞👍🏻 收藏 📂 加关注✨


目录

  • 一、const成员
  • 二、取地址及const取地址操作符重载
    • 2.1 特殊使用情况

一、const成员

const修饰的“成员函数”称之为const成员函数, const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

我们来看看下面的代码:

#include <iostream>
using namespace std;class Date
{
public:Date(int year, int month, int day){Year = year;Month = month;Day = day;}void Print(Date* const this){cout << Year << "-" << Month << '-' << Day << endl;}private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);d1.Print();const Date d2(2023, 5, 21);d2.Print();return 0;
}

在这里插入图片描述

  • 以上代码对于d2是编译不通过的,这是为什么呢?
  1. 当对象d1调用成员函数Print时,编译器会隐式自动地将该对象的地址作为第一个参数传递给成员函数,因此,在一个类的成员函数内部,可以使用this指针来访问该对象的成员变量和成员函数。this指针的类型:类名* const this,const修饰this,即成员函数中,不能修改this指针;
  2. 而对于对象d2,当调用成员函数时,编译器同样会自动将该对象作为第一个参数传递给成员函数,但不同的是,由于const修饰d2,因此this指针的类型应该是const Date* const this,因此不能给*this赋值,所以导致了报错。

因此,为了解决以上问题。C++是允许在函数后加上一个const,这样就能正常运行了:

#include <iostream>
using namespace std;class Date
{
public:Date(int year, int month, int day){Year = year;Month = month;Day = day;}void Print() const{cout << this->Year << "-" << this->Month << '-' << this->Day << endl;}private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);d1.Print();const Date d2(2023, 5, 21);d2.Print();return 0;
}

【程序结果】

在这里插入图片描述

【总结】

  1. 成员函数在后加上const后,普通和const修饰的对象都能用
  2. 但注意:由于const修饰的对象,其形参中的*thisconst修饰,也就说明不能对*this进行修改。因此,如果成员函数内要修改对象成员变量的函数千万不要用const修饰对象。
  3. 如果声明和定义分离,声明的函数和定义的函数都要加const

二、取地址及const取地址操作符重载

首先先来看看一下代码:

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){Year = year;Month = month;Day = day;}
private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);const Date d2(2023, 5, 21);cout << &d1 << endl;cout << &d2 << endl;return 0;
}

【程序结果】

在这里插入图片描述

通过以上结果我们发现:const取地址操作符重载不用重新定义,编译器默认会生成。

当然,如果想自己定义也是可以的,代码如下:

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){Year = year;Month = month;Day = day;}Date* operator&(){return this;}const Date* operator&() const{return this;}private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);const Date d2(2023, 5, 21);cout << &d1 << endl;cout << &d2 << endl;return 0;
}

【程序结果】

在这里插入图片描述

需要注意的是:重载const 取地址操作符时,返回的指针类型必须是const指针,否则会导致编译错误。

2.1 特殊使用情况

虽然这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如 不想让别人获取到指定的内容

#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1, int month = 1, int day = 1){Year = year;Month = month;Day = day;}Date* operator&(){return nullptr;}const Date* operator&() const{return nullptr;}
private:int Year; int Month; int Day; 
};int main()
{Date d1(2023, 5, 20);const Date d2(2023, 5, 21);cout << &d1 << endl;cout << &d2 << endl;return 0;
}

【程序结果】

在这里插入图片描述

http://www.15wanjia.com/news/3158.html

相关文章:

  • 网站开发保密协议平台外宣推广技巧
  • 做复刻衣服买网站中国网站排名查询
  • 微商的自己做网站叫什么软件阿里云域名查询
  • c 网站开发网易云课堂百度云下载百度助手手机下载
  • 社交网站设计腾讯搜索引擎入口
  • 专门做尿不湿的网站个人网页制作成品
  • 微信做网站seo和sem的关系
  • 校园网网站建设实训报告seo运营工作内容
  • 韶关做网站的公司网站域名注册查询
  • 免费网站模板建站网站搜索优化找哪家
  • 孙俪做的网站广告西地那非片能延时多久有副作用吗
  • 营销型网站建设要懂代码吗seo推广方式是什么呢
  • 长沙最新死亡事件宁波seo哪家好快速推广
  • 沈阳市网站建设公司最新国际新闻10条
  • 怎么做让网站排名靠前seo怎么搞
  • 如何做专业的模板下载网站互联网广告推广公司
  • 国外做免费的视频网站有哪些精准推广引流5000客源
  • 做网站给源码吗青岛网站建设优化
  • asp自助建站系统免费单页网站在线制作
  • 平面广告设计培训学校上海网络关键词优化
  • python创建网站武汉seo首页优化技巧
  • 网站做图片58百度搜索引擎
  • 太原网站制作哪家不错网络销售平台
  • 做淘宝货源网站杭州百度推广代理商
  • 智慧团建注册入口搜索引擎优化怎么做
  • 北京公司名称seo运营培训
  • 福建省住房和城乡建设厅网站电话浏览器老是出现站长工具
  • 英文视频网站如何做外链百度网盘下载慢怎么解决
  • 公众号和网站百度手机网页版入口
  • 网站管理员怎么联系广告传媒公司