this指针和空指针1,this指针1 this指针解决名称冲突。 2返回对象本身使用*this、null指针调用成员函数3和const限定成员函数
在上一篇文章中,我们发现c分别存储了成员变量和成员函数
每个非静态成员函数只能生成一个函数实例。 也就是说,同一类型的多个对象共享一个代码。 问题是: 块代码是如何区分该对象并调用自身的呢?
c通过提供特殊的对象指针、this指针来解决上述问题。this指针指向被调用的成员函数所属的对象
this指针表示所有非静态成员函数中的-类型指针
this指针不需要定义,直接使用即可
this指针的用途:
如果形参和成员变量具有相同的名称,则可以使用this指针进行区分
要在类的非静态成员函数中返回对象本身,请使用return this
另一方面,this指针1 this指针解决名称冲突# includeiostreamusingnamespacestd; class person { public : person (intage ) {age=age; (}int age; (; //1解决名称冲突的void test1 () {personp1 ) 18 ); cout 'p1的年龄为=' p1.age endl; (}int main ) ) {test1); 返回0; }输出年龄乱码
分析
如果将光标悬停在形参age上,则三个age均为灰色,系统会识别这三个age是相同的数据
解决方案1 :
写成员变量和形参是将m_相加来区分的
class person { public : person (intage ) {m_Age=age; (}int m_Age; (;
解决方案2 :
this指针指向被调用的成员函数所属的对象
解决class Person{public://1名称冲突的person(intage ) {//this指针是被调用的成员函数所属的对象this-age=age; (}int age; (;
2返回的对象本身是用* this class person { public ://1解决名称冲突的Person(intage ) {//this指针,调用的成员函数所属的对象this-/this //2返回对象本身为*thisvoidpersonaddage(personp ) {this-age =p.age; (}int age; (; //1解决名称冲突的void test1 () {personp1 ) 18 ); cout 'p1的年龄为=' p1.age endl; //2返回对象本身为*ThisPersonP2(10 ); p2 .个人身份验证(P1; cout 'p2的年龄为=' p2.age endl; }
我现在想在年龄之后继续累积,犯错误
函数test1 ) )是无void返回值类型,调用结束后不能再调用
P2.personaddage(P1 ).personaddage (P1 ).personaddage (P1 ) );
如果每次调用结束时都可以返回p2,则可以继续调用personaddage(p1 )。
//2返回对象本身在*thispersonpersonaddage (personp ) {//this中指向p2的指针,* this是p2对象的主体this-age =p.age; 返回* This; }完整代码
# include iostream # includestringusingnamespacestd; //1解决名称冲突//2返回对象本身。 (thisclassPerson )用于解决公共://1名称冲突的Person(intage ) {//this指针指向被调用的成员函数所属的对象this-age=age。 //2返回的对象本身在*thispersonpersonaddage (personp ) {//this中指向p2的指针,* this是p2对象的主体this-age =p.age; 返回* This; (}int age; (; //1解决名称冲突的void test1 () {personp1 ) 18 ); cout 'p1的年龄为=' p1.age endl; //2返回对象本身为*ThisPersonP2(10 ); P2.personaddage(P1 ).personaddage (P1 ).personaddage (P1 ) ); cout 'p2的年龄为=' p2.age endl; (}int main ) ) {test1); 返回0; }
二、空指针调用成员函数C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
如果用到this指针,需要加以判断保证代码的健壮性
#include<iostream>using namespace std;//空指针调用成员函数class Person{public:void showClassName(){cout << "This is Person class" << endl;}void showPersonAge(){//报错原因是因为传入的是空指针cout << "age=" <<m_Age<< endl;}int m_Age;};void test1(){Person* p = NULL;p->showClassName();//p->showPersonAge();//报错原因是因为传入的是空指针}int main() {test1();return 0;}
//报错原因是因为传入的是空指针
if(this==NULL) return; //解决空指针出错
void showPersonAge(){//报错原因是因为传入的是空指针if(this==NULL) return; //解决空指针出错cout << "age=" <<m_Age<< endl;}完整代码
#include<iostream>using namespace std;//空指针调用成员函数class Person{public:void showClassName(){cout << "This is Person class" << endl;}void showPersonAge(){//报错原因是因为传入的是空指针if(this==NULL) return; //解决空指针出错cout << "age=" <<m_Age<< endl;}int m_Age;};void test1(){Person* p = NULL;p->showClassName();p->showPersonAge();//报错原因是因为传入的是空指针}int main() {test1();return 0;} 三、const修饰成员函数常函数:
·成员函数后加const后我们称为这个函数为常函数
.常函数内不可以修改成员属性
·成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
·声明对象前加const称该对象为常对象
·常对象只能调用常函数
解决方法:
成员属性声明时加关键字mutable,在常函数中才可以修改
mutable int m_B;//特殊变量,即使在常函数中,也可修饰这个值,加关键字mutable
const Person p;//在对象前加const变常对象
//常对象void test2(){const Person p;//在对象前加const变常对象//p.m_A = 100;//报错p.m_B = 100;//m_B是特殊值,在常对象下也可以修改//常对象只能调用常函数p.showPerson();//p.func();//常对象不可以调用普通成员函数,因为普通成员函数可以修改属性}完整代码
#include<iostream>using namespace std;//常函数//常对象class Person{public://this指针的本质是指针常量,指针的指向是不可以修改的//const Person *const this;//在成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改void showPerson() const{//m_A = 100; //常函数内不可以修改成员属性//this->m_A = 100;//this = NULL;m_B = 100; //成员属性声明时加关键字mutable,在常函数中才可以修改}void func(){}int m_A;mutable int m_B;//特殊变量,即使在常函数中,也可修饰这个值,加关键字mutable};//常函数void test1(){Person p;p.showPerson();}//常对象void test2(){const Person p;//在对象前加const变常对象//p.m_A = 100;//报错p.m_B = 100;//m_B是特殊值,在常对象下也可以修改//常对象只能调用常函数p.showPerson();//p.func();//常对象不可以调用普通成员函数,因为普通成员函数可以修改属性}int main() {test1();return 0;}参考:黑马程序员
哔哩哔哩 黑马程序员