0
点赞
收藏
分享

微信扫一扫

C++ 27.类与对象—对象特性—对象模型与this指针

代码小姐 2022-03-11 阅读 73

成员变量与成员函数分开存储

this指针的用途

空指针访问成员函数

const修饰成员函数


成员变量与成员函数分开存储

在c++中,类内的成员变量和成员函数分开存储。只有非静态成员变量才属于类的对象。

#include<iostream>
using namespace std;
#include<string>
//类对象作为类成员
class Person
{
	int m_A;  //非静态成员变量,属于类的对象上的数据
	static int m_B;  //静态成员变量。不属于类的对象上。
	void func (){}  //非静态成员函数,不属于类的对象上。
	static void func(){}  //静态成员函数,不属于类的对象上。
};
int Person::m_B = 0;//静态成员变量类内声明,类外初始化

void test01()
{
	Person p;
	//空对象占用内存空间为:1
	//C++编译会给每个空对象也分配一个字节空间,为了区分空对象占内存的位置。每个空对象也应该有一个独一无二的内存空间
	cout << "size of p = " << sizeof(p) << endl;
}

void test02()
{
	Person p;
	//非静态成员变量占用内存空间为:4。因为int
	
	cout << "size of p = " << sizeof(p) << endl;
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

this指针的用途

在c++中,通过this指针, 1.解决对象名称冲突   2.返回对象本身用*this
this指针指向被调用的成员函数所属的对象。
this指针是隐含每一个非静态成员函数内的一种指针。
this指针不需要定义,直接使用即可。

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

class Person
{
public:
	Person(int age)
	{
		//this指针指向  (p1)被调用的成员函数  所属的对象。
		this->age = age;//不可写为 age = age;
	}

	Person& PersonAddAge(Person &p)//要用 Person&引用 的方式,返回对象的本体
	{
		this->age += p.age;
		//this 指向p2的指针,而*this指向的就是p2这个对象本体
		return *this;
	}
	int age;
};
//1.解决对象名称冲突
void test01()
{
	Person p1(18);
	cout << "p1的年龄为: " << p1.age << endl;
}
//2.返回对象本身用*this
void test02()
{
	Person p1(10);
	Person p2(5);
	//p2.PersonAddAge(p1);//这个p2是15
	//链式编程思想。用this指针
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);//这个p2是35

	cout << "p2的年龄为: " << p2.age << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

空指针访问成员函数

c++中空指针也可以调用成员函数,但是也要注意有没有用到this指针。
如果用到this指针,需要加以判断保证代码的健壮性。

#include<iostream>
using namespace std;

//空指针调用成员函数
class Person
{
public:
	void showClassName()
	{
		cout << "this is Person class" << endl;
	}
	void showPersonAge()
	{
		//报错是因为传入的指针为NULL,所以加入判断语句
		if (this == NULL)
		{
			return;
		}
		cout << "age = " << this->m_Age << endl;//m_Age等同于this->m_Age,是个空指针
	}
	int m_Age;
};

void test01()
{
	Person *p = NULL;
	p->showClassName();
	p->showPersonAge();
}
int main()
{
	test01();
	
	system("pause");
	return 0;
}

const修饰成员函数

常函数
1.成员函数后加const后称为常函数
2.常函数内不可以修改成员属性
3.成员属性声明时,加关键字mutable后,在常函数中依然可以修改
常对象
1.声明对象前加const称该对象为常对象
2.常对象只能调用常函数

#include<iostream>
using namespace std;

//常函数
class Person
{
public:
	//this指针的本质 是指针常量 指针的指向是不可以修改的
	//Person * const this;//指针指向不可修改
	//const Person * const this;//指针指向不可修改,指针指向的值也不可以修改。
	//成员函数后面加const,修饰的是this指向,让指针指向的值也不可以修改
	void showPerson() const
	{
		this->m_B = 100;
		//this->m_A = 100;// this->m_A相当于m_A
		//this = NULL;//this指针是不可以修改指针的指向,但可以修改指针的值
	}

	void func()
	{

	}
	int m_A;
	mutable int m_B;//特殊变量,即使在常函数中,也可以修改这个值.加关键字mutable
};

void test01()
{
	Person p;
	p.showPerson;
}

//常对象
void test02()
{
	const Person p;//在对象前加const,变为常对象
	//p.m_A = 100;
	p.m_B = 100;//m_B是特殊值,在常对象下也可修改

	//常对象只能调用常函数
	p.showPerson();
	//p.func();//常对象 不可以调用普通成员,因为普通成员函数可以修改属性
}
int main()
{
	test01();

	system("pause");
	return 0;
}
举报

相关推荐

0 条评论