c++ this指针与空指针调用类方法以及常函数

一、this指针

说明

1、c++的成员变量与成员内函数是分开存储
2、每一个非静态成员函数只会诞生一份函数实例,多个同类型的队形公用的是同一份成员函数的代码
3、this指向调用这一份成员函数代码的对象实例
4、this是一个隐藏的指向对象实例的一个指针,无需定义直接使用即可

作用

1、可以区别形参与成员变量(当成员函数的形参与类成员变量同名时)
2、在类的非静态成员函数中返回对象本身,return *this 

例子一

#include <iostream>
#include <string.h>

using namespace std;

//this指针
//1、c++的成员变量与成员内函数是分开存储
//2、每一个非静态成员函数只会诞生一份函数实例,多个同类型的队形公用的是同一份成员函数的代码
//3、this指向调用这一份成员函数代码的对象实例
//4、this是一个隐藏的指向对象实例的一个指针,无需定义直接使用即可

//作用
//1、可以区别形参与成员变量(当成员函数的形参与类成员变量同名时)
//2、在类的非静态成员函数中返回对象本身,return *this 
class Persion
{
public:
	void func01(int age)
	{
		//age = age;//此时复制符号的做优都是同一个变量
		this->age = age;
		this->m_Age = age;

	}
	/*Persion funcAddMAge(Persion &p)
	{
		this->m_Age += p.m_Age;
		return *this;
	}*/
	Persion funcAddMAge(Persion& p)
	{
		this->m_Age += p.m_Age;
		return *this;
	}
	int age = age;
	int m_Age;//m_ 为member
	string m_Name;
};

void main()
{
	Persion p;
	p.func01(10);
	cout << "func01:" << p.age << endl;//func01:10
	Persion p2;
	p2.func01(10);
	cout << "func02:" << p2.m_Age << endl;//func02:10
	p2.funcAddMAge(p).funcAddMAge(p).funcAddMAge(p);
	//Persion funcAddMAge(Persion &p)如果返回值为对象本身,则每次返回的都是一个新的对象
	//cout << "func02:" << p2.m_Age << endl;//func02:20
	cout << "func02:" << p2.m_Age << endl;//func02:20

}

例子二

#include <iostream>
#include <string.h>

using namespace std;

//this指针
//1、c++的成员变量与成员内函数是分开存储
//2、每一个非静态成员函数只会诞生一份函数实例,多个同类型的队形公用的是同一份成员函数的代码
//3、this指向调用这一份成员函数代码的对象实例
//4、this是一个隐藏的指向对象实例的一个指针,无需定义直接使用即可

//作用
//1、可以区别形参与成员变量(当成员函数的形参与类成员变量同名时)
//2、在类的非静态成员函数中返回对象本身,return *this 
class Persion
{
public:
	void func01(int age)
	{
		//age = age;//此时复制符号的做优都是同一个变量
		this->age = age;
		this->m_Age = age;

	}
	/*Persion funcAddMAge(Persion &p)
	{
		this->m_Age += p.m_Age;
		return *this;
	}*/
	Persion& funcAddMAge(Persion& p)//返回的是对象本身
	{
		this->m_Age += p.m_Age;
		return *this;
	}
	int age = age;
	int m_Age;//m_ 为member
	string m_Name;
};

void main()
{
	Persion p;
	p.func01(10);
	cout << "func01:" << p.age << endl;//func01:10
	Persion p2;
	p2.func01(10);
	cout << "func02:" << p2.m_Age << endl;//func02:10
	p2.funcAddMAge(p).funcAddMAge(p).funcAddMAge(p);
	//Persion funcAddMAge(Persion &p)如果返回值为对象本身,则每次返回的都是一个新的对象
	//cout << "func02:" << p2.m_Age << endl;//func02:20
	cout << "func02:" << p2.m_Age << endl;//func02:40

}

 二、空指针调用类方法

#include <iostream>
#include <string.h>
using namespace std;
class Persion
{
public:

	void showClassName()
	{
		cout << "class name is Persion" << endl;
	}
	void showClassAge()
	{
		/*if (this == NULL) {//可以避免传入空指针
		
			return;
		}*/
		//这里隐藏了额this 此处相当于this->m_Age
		cout << m_Age << endl;
	
	}
	int m_Age = 10;
};

void main()
{
	Persion * p = NULL;
	p->showClassName();
	p->showClassAge();//不能的出正确预期

	

}

三、const常函数

说明    

this指针的本质,是常量指针 Persion * const this,指针的变量this的内容,即指向不可改变
但是只想的内容是可以已修改的,所以可以使用this->m_Age = 100;
如果成员函数后面添加const  则变量const Persion * const this,指针直系那个以及内容都不可修改
只影响添加了const关键字的常函数

例外

特殊变量,即使在常函数中,也可以修改,需要添加关键字mutable

#include <iostream>
#include <string.h>
using namespace std;
class Persion
{
public:
	//this指针的本质,是常量指针 Persion * const this,指针的变量this的内容,即指向不可改变
	//但是只想的内容是可以已修改的,所以可以使用this->m_Age = 100;
	//如果成员函数后面添加const  则变量const Persion * const this,指针直系那个以及内容都不可修改
	//只影响添加了const关键字的常函数
	void showPersion() const
	{
		//this->m_Age = 100;
		//this = NULL;
		this->m_Name = "WQ";

	}
	void showPersion02()
	{
		this->m_Age = 100;
	}

	int m_Age;
	mutable string m_Name;//特殊变量,即使在常函数中,也可以修改,需要添加关键字mutable

};
void main()
{
	Persion p;
	p.showPersion();



}

常对象例子

常对象只能调用常函数,不能调用普通函数,因为普通函数可能修改 

#include <iostream>
#include <string.h>
using namespace std;
class Persion
{
public:
	//this指针的本质,是常量指针 Persion * const this,指针的变量this的内容,即指向不可改变
	//但是只想的内容是可以已修改的,所以可以使用this->m_Age = 100;
	//如果成员函数后面添加const  则变量const Persion * const this,指针直系那个以及内容都不可修改
	//只影响添加了const关键字的常函数
	void showPersion() const
	{
		//this->m_Age = 100;
		//this = NULL;
		this->m_Name = "WQ";

	}
	void showPersion02()
	{
		this->m_Age = 100;
	}

	int m_Age;
	mutable string m_Name;//特殊变量,即使在常函数中,也可以修改,需要添加关键字mutable

};
void main()
{
	Persion p;
	p.showPersion();
	
	const Persion p2;//常对象
	p2.showPersion();//可以调用普通函数
	p2.m_Age = 100;//err
	p2.m_Name = "wq";//正常
	p2.showPersion02();//err 常对象只能调用常函数,不能调用普通函数,因为普通函数可能修改




}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值