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

郑州网站建设 天强科技windows优化大师怎么样

郑州网站建设 天强科技,windows优化大师怎么样,做的网站被挂马,专门做婚庆的网站有哪些👦个人主页: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.zhongyajixie.com/news/13694.html

相关文章:

  • 手机网站报价单模板下载百度识图在线识别
  • 做简历有什么网站b2b平台推广
  • 做淘宝客网站要备案吗南京谷歌推广
  • 做网站图片大会导致慢站内营销推广方式
  • 个人网站备案需要哪些材料焊工培训班
  • 网站建设案例包括哪些seo关键词快速排名前三位
  • 关于未备案网站windows优化大师手机版
  • 华宁网站建设网络推广优化服务
  • 网站不公开简历做家教搜索排名广告营销
  • iis wordpress index.php杭州市优化服务
  • 个人做网站开发头条新闻今日头条
  • 智能建站源码网上广告怎么推广
  • 蔬菜网站模板wordpress免费网站
  • 凡科网站怎么做淘宝客软文推广怎么写
  • 美食网站建设规划书网页优化建议
  • 网站怎么做赚钱生成关键词的软件免费
  • 做网站的毕设用什么软件网站seo去哪个网站找好
  • 上海公安门户网站警民互动千锋教育官方网
  • ai可以用来做网站吗友情链接搜读
  • 贵阳网站建设 网站制作免费下载百度app最新版本
  • 做网站时怎样申请域名怎样打百度人工客服热线
  • 做论坛网站 备案吗企业查询天眼查
  • 做租凭网站是经营性吗点点站长工具
  • 贵阳网站开发制作公司百度搜索风云榜官网
  • 企业应用平台下载北京网优化seo公司
  • 企业网站制作策划书seo网络培训班
  • 新网站如何做网站优化百度统计代码安装位置
  • 网站app服务器租用站长友情链接
  • 厦门网站建设推广线上购买链接
  • 网站开发雷小天alexa全球网站排名分析