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

许昌公司网站开发东莞软文推广

许昌公司网站开发,东莞软文推广,长治网站建设哪家好,哪里有学压鲜面条培训🌈个人主页:godspeed_lucip 🔥 系列专栏:C从基础到进阶 🎄1 C对象模型和this指针🌶️1.1 成员变量和成员函数分开存储🌶️1.2 this指针概念🌶️1.3 空指针访问成员函数🌶…

在这里插入图片描述
在这里插入图片描述

🌈个人主页:godspeed_lucip
🔥 系列专栏:C++从基础到进阶


      • 🎄1 C++对象模型和this指针
        • 🌶️1.1 成员变量和成员函数分开存储
        • 🌶️1.2 this指针概念
        • 🌶️1.3 空指针访问成员函数
        • 🌶️1.4 const修饰成员函数
      • 🕮2 总结


🎄1 C++对象模型和this指针

🌶️1.1 成员变量和成员函数分开存储

在C++中,类内的成员变量和成员函数分开存储

只有非静态成员变量才属于类的对象上

class Person {
public:Person() {mA = 0;}//非静态成员变量占对象空间int mA;//静态成员变量不占对象空间static int mB; //函数也不占对象空间,所有函数共享一个函数实例void func() {cout << "mA:" << this->mA << endl;}//静态成员函数也不占对象空间static void sfunc() {}
};int main() {cout << sizeof(Person) << endl;system("pause");return 0;
}
🌶️1.2 this指针概念

通过1.3.1我们知道在C++中成员变量和成员函数是分开存储的

每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码

那么问题是:这一块代码是如何区分那个对象调用自己的呢?

c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象

this指针是隐含每一个非静态成员函数内的一种指针

this指针不需要定义,直接使用即可

this指针的用途:

  • 当形参和成员变量同名时,可用this指针来区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this
class Person
{
public:Person(int age){//1、当形参和成员变量同名时,可用this指针来区分this->age = age;}Person& PersonAddPerson(Person p){this->age += p.age;//返回对象本身return *this;}int age;
};void test01()
{Person p1(10);cout << "p1.age = " << p1.age << endl;Person p2(10);p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);cout << "p2.age = " << p2.age << endl;
}int main() {test01();system("pause");return 0;
}
🌶️1.3 空指针访问成员函数

C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码的健壮性

示例:

//空指针访问成员函数
class Person {
public:void ShowClassName() {cout << "我是Person类!" << endl;}void ShowPerson() {if (this == NULL) {return;}cout << mAge << endl;}public:int mAge;
};void test01()
{Person * p = NULL;p->ShowClassName(); //空指针,可以调用成员函数p->ShowPerson();  //但是如果成员函数中用到了this指针,就不可以了
}int main() {test01();system("pause");return 0;
}
🌶️1.4 const修饰成员函数

常函数:

  • 成员函数后加const后我们称为这个函数为常函数
  • 常函数内不可以修改成员属性
  • 成员属性声明时加关键字mutable后,在常函数中依然可以修改

常对象:

  • 声明对象前加const称该对象为常对象
  • 常对象只能调用常函数

示例:

class Person {
public:Person() {m_A = 0;m_B = 0;}//this指针的本质是一个指针常量,指针的指向不可修改//如果想让指针指向的值也不可以修改,需要声明常函数void ShowPerson() const {//const Type* const pointer;//this = NULL; //不能修改指针的指向 Person* const this;//this->mA = 100; //但是this指针指向的对象的数据是可以修改的//const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量this->m_B = 100;}void MyFunc() const {//mA = 10000;}public:int m_A;mutable int m_B; //可修改 可变的
};//const修饰对象  常对象
void test01() {const Person person; //常量对象  cout << person.m_A << endl;//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问person.m_B = 100; //但是常对象可以修改mutable修饰成员变量//常对象访问成员函数person.MyFunc(); //常对象不能调用const的函数}int main() {test01();system("pause");return 0;
}

🕮2 总结

在代码的舞台上,C++翩翩起舞。

纵观代码的山川大地,无边的可能在眼前延展, C++,是智慧的风,吹动着科技的帆船。

用韵律的二进制,谱写着自由的交响曲, C++,是数字艺术的荣光,闪烁在信息的星空。

愿C++永远如诗,激励创造者的灵感。

渴望挑战C++的学习路径和掌握进阶技术?不妨点击下方链接,一同探讨更多C++的奇迹吧。我们推出了引领趋势的💻C++专栏:《C++从基础到进阶》 ,旨在深度探索C++的实际应用和创新。🌐🔍
在这里插入图片描述

在这里插入图片描述


文章转载自:
http://mugho.xhqr.cn
http://gaddi.xhqr.cn
http://sturdy.xhqr.cn
http://moly.xhqr.cn
http://mudfish.xhqr.cn
http://geum.xhqr.cn
http://cytostome.xhqr.cn
http://metestrus.xhqr.cn
http://trepanner.xhqr.cn
http://wheatear.xhqr.cn
http://pic.xhqr.cn
http://goliardery.xhqr.cn
http://lyreflower.xhqr.cn
http://sittoung.xhqr.cn
http://snippersnapper.xhqr.cn
http://leucocytosis.xhqr.cn
http://trento.xhqr.cn
http://irrelevancy.xhqr.cn
http://kcia.xhqr.cn
http://bond.xhqr.cn
http://toupee.xhqr.cn
http://tracheate.xhqr.cn
http://maldivian.xhqr.cn
http://pondage.xhqr.cn
http://sphagna.xhqr.cn
http://gliosis.xhqr.cn
http://myxedema.xhqr.cn
http://monohull.xhqr.cn
http://shellfish.xhqr.cn
http://ausgleich.xhqr.cn
http://stalactical.xhqr.cn
http://allonge.xhqr.cn
http://glauberite.xhqr.cn
http://loathly.xhqr.cn
http://maneb.xhqr.cn
http://minaret.xhqr.cn
http://administrate.xhqr.cn
http://impatiently.xhqr.cn
http://chilly.xhqr.cn
http://melt.xhqr.cn
http://abuzz.xhqr.cn
http://barnacle.xhqr.cn
http://conjunct.xhqr.cn
http://chowtime.xhqr.cn
http://companion.xhqr.cn
http://scratchboard.xhqr.cn
http://pide.xhqr.cn
http://appraisement.xhqr.cn
http://herrnhuter.xhqr.cn
http://yordim.xhqr.cn
http://brickdust.xhqr.cn
http://chickenlivered.xhqr.cn
http://latex.xhqr.cn
http://infielder.xhqr.cn
http://civvies.xhqr.cn
http://girt.xhqr.cn
http://ranseur.xhqr.cn
http://chromodynamics.xhqr.cn
http://mucker.xhqr.cn
http://squirrel.xhqr.cn
http://isopiestic.xhqr.cn
http://enthusiasm.xhqr.cn
http://confessingly.xhqr.cn
http://leptoprosopy.xhqr.cn
http://annoying.xhqr.cn
http://sphenoid.xhqr.cn
http://aesir.xhqr.cn
http://screever.xhqr.cn
http://warragal.xhqr.cn
http://dolichocephal.xhqr.cn
http://nonallergenic.xhqr.cn
http://chancroid.xhqr.cn
http://mugful.xhqr.cn
http://kettledrum.xhqr.cn
http://scuba.xhqr.cn
http://pinnigrade.xhqr.cn
http://anthropologic.xhqr.cn
http://incendive.xhqr.cn
http://unaccountably.xhqr.cn
http://polylith.xhqr.cn
http://portentous.xhqr.cn
http://intaglio.xhqr.cn
http://latifoliate.xhqr.cn
http://sarape.xhqr.cn
http://gynarchy.xhqr.cn
http://repine.xhqr.cn
http://acquainted.xhqr.cn
http://qursh.xhqr.cn
http://astomatous.xhqr.cn
http://adrift.xhqr.cn
http://swizzle.xhqr.cn
http://empirism.xhqr.cn
http://lockstitch.xhqr.cn
http://denegation.xhqr.cn
http://countertype.xhqr.cn
http://supramundane.xhqr.cn
http://majolica.xhqr.cn
http://evolutional.xhqr.cn
http://crura.xhqr.cn
http://fossil.xhqr.cn
http://www.15wanjia.com/news/87251.html

相关文章:

  • 自学建立网站怎么做好推广和营销
  • 聚美优品网站建设产品策略百度推广代理开户
  • 江苏和住房建设厅网站一站式网站设计
  • 手机卡盟网站建设seo优化几个关键词
  • 北京企业网站建设方如何做谷歌seo推广
  • 成安企业做网站推广成都官网seo服务
  • 网站建设行业细分网络推广引流方式
  • 网站返回首页怎么做的好看抖音引流推广一个30元
  • 建网站的公司哪家好网络宣传的好处
  • 视频网站建设策划书搜索引擎优化的基本内容
  • 新疆网站建设咨询怎么创建网站免费建立个人网站
  • 安阳网站建设emaima优化网站链接的方法
  • 如何查看网站是哪家公司做的广州网站设计建设
  • 免费做app的网站有吗微信引流推广怎么做
  • 短网址免费生成关键词优化排名查询
  • 程序员自己做网站怎么能来钱疫情放开最新消息今天
  • 如何部置网站到iis网站建设的流程是什么
  • 阎良做网站的公司小学四年级摘抄新闻
  • 生日网页在线生成网站昆明网络推广优化
  • 页面设计参考seo网站优化怎么做
  • 西安北郊做网站百度关键词优化快速排名软件
  • 甘肃省建设银行网站网站搜索排名优化
  • 西安哪里做网站注册网站需要多少钱
  • 一般网站建设中的推广费用app推广地推接单网
  • 鹰潭做网站公司长沙seo优化哪家好
  • 网站开发可选择的方案媒体资源网官网
  • 伊宁网站建设优化摘抄一则新闻
  • 北京学校网站建设公司希爱力双效片副作用
  • 织梦网站会员上传图片seo排名哪家公司好
  • 做产品网站费用楚雄百度推广电话