温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在C++中使用this指针和空指针

发布时间:2021-04-29 16:15:10 来源:亿速云 阅读:195 作者:Leah 栏目:开发技术

本篇文章为大家展示了怎么在C++中使用this指针和空指针,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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

this指针是隐含每一个非静态成员函数内的—种指针
this指针不需要定义,直接使用即可

this指针的用途:

  • 当形参和成员变量同名时,可用this指针来区分

  • 在类的非静态成员函数中返回对象本身,可使用return this

怎么在C++中使用this指针和空指针

一、this指针

1 this指针解决名称冲突

#include <iostream>
using namespace std;
class Person
{
public:
	Person(int age)
	{
		age = age;
	}
	int age;

};
//1 解决名称冲突
void test1()
{
	Person p1(18);
	cout << "p1的年龄为=" << p1.age << endl;
}
int main() 
{
	test1();
	return 0;
}

输出年龄乱码

怎么在C++中使用this指针和空指针

分析

怎么在C++中使用this指针和空指针

光标放在形参age上,发现三个age 都是灰色,系统认为这个三个age 是同一数据

解决1:

将成员变量和形参书写是上加m_区分

class Person
{
public:
	Person(int age)
	{
		m_Age = age;
	}
	int m_Age;

};

怎么在C++中使用this指针和空指针

解决2:

this指针指向 被调用的成员函数 所属对象

class Person
{
public:
	//1 解决名称冲突
	Person(int age)
	{
		//this指针指向 被调用的成员函数 所属对象
		this->age = age;
	}
	int age;

};

怎么在C++中使用this指针和空指针

怎么在C++中使用this指针和空指针

怎么在C++中使用this指针和空指针

2 返回对象本身用*this

class Person
{
public:
	//1 解决名称冲突
	Person(int age)
	{
		//this指针指向 被调用的成员函数 所属对象
		this->age = age;
	}
	
	//2 返回对象本身用*this
	void PersonAddAge(Person &p)
	{
		this->age += p.age;
	}

	int age;

};
//1 解决名称冲突
void test1()
{
	Person p1(18);
	cout << "p1的年龄为=" << p1.age << endl;

	//2 返回对象本身用*this
	Person p2(10);
	p2.PersonAddAge(p1);
	cout << "p2的年龄为=" << p2.age << endl;
}

怎么在C++中使用this指针和空指针

现在想要年龄后面继续累加,出错

怎么在C++中使用this指针和空指针

函数test1()是void无返回值型,调用完毕就不能再调用了

p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
如果每次调用完毕,可以返回到p2,就可以继续再调用PersonAddAge(p1);

//2 返回对象本身用*this
Person &PersonAddAge(Person &p)
{
	//this指向p2的指针,而*this指向的就是p2这个对象的本体
	this->age += p.age;
	return *this;
}

完整代码

#include <iostream>
#include<string>
using namespace std;

//1 解决名称冲突

//2 返回对象本身用*this

class Person
{
public:
	//1 解决名称冲突
	Person(int age)
	{
		//this指针指向 被调用的成员函数 所属对象
		this->age = age;
	}
	
	//2 返回对象本身用*this
	Person &PersonAddAge(Person &p)
	{
		//this指向p2的指针,而*this指向的就是p2这个对象的本体
		this->age += p.age;
		return *this;
	}

	int age;

};
//1 解决名称冲突
void test1()
{
	Person p1(18);
	cout << "p1的年龄为=" << p1.age << endl;

	//2 返回对象本身用*this
	Person p2(10);
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
	cout << "p2的年龄为=" << p2.age << endl;
}

int main() 
{
	test1();
	return 0;
}

怎么在C++中使用this指针和空指针

二、空指针调用成员函数

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;
}

怎么在C++中使用this指针和空指针

//报错原因是因为传入的是空指针

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;
}

怎么在C++中使用this指针和空指针

三、const修饰成员函数

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

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

怎么在C++中使用this指针和空指针

怎么在C++中使用this指针和空指针

解决方法:
成员属性声明时加关键字mutable,在常函数中才可以修改
mutable int m_B;//特殊变量,即使在常函数中,也可修饰这个值,加关键字mutable

//常函数
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,在常函数中才可以修改
	}

	int m_A;
	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;
}

上述内容就是怎么在C++中使用this指针和空指针,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI