C++ 类和对象–对象模型和this指针

4.3 C++对象模型和this指针

4.3.1 成员变量和成员函数分开存储

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

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

#include <iostream>
using namespace std;

// 成员变量 和成员函数 分开存储的

class Person
{
	int m_A; // 非静态成员变量  属于类的对象上的

	string m_C;

	static int m_B; // 静态成员变量  不属于类的对象上

	void func() // 非静态成员函数  不属于类的对象上
	{

	}

	static void func2() // 静态成员函数 不属实类的对象上
	{

	} 
};

int Person::m_B = 100;

void test01()
{
	Person p;

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

void test02()
{
	Person p;

	cout << "sizeof p = " << sizeof(p) << endl;
}

int main()
{

	// test01();
	
	test02();

	system("pause");

	return 0;
}

4.3.2 this指针概念

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

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

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

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

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

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

this指针的用途:

  • 当形参和成员变量同名时,可用this指针来区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this
#include <iostream>
using namespace std;

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

	Person& PersonAddAge(Person &p)
	{
		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(10);

	
	// 如果调用的函数返回的是 Person
	// 
	// p2.PersonAddAge(p1); 修改了 p2 的 age (p2.age += p1.age)
	// 
	// p2.PersonAddAge(p1).PersonAddAge(p1); 
	// 先调用了 p2.PersonAddAge(p1); 返回了一个拷贝 p2 的函数,然后 p2`.PersonAddAge(p1);
	// 
	// p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1); 
	// 先调用了 p2.PersonAddAge(p1); 返回了一个拷贝 p2 的函数,然后 p2`.PersonAddAge(p1), 最后 p2``.PersonAddAge(p1)
	// p2` 和 p2`` 均为新创建的函数,不会影响 p2 的值


	// 如果调用的函数返回的是 Person&
	// p2.PersonAddAge(p1); 修改了 p2 的 age (p2.age += p1.age)
	// 
	// p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1); 因为返回的是 p2 的地址,所以函数的调用是这样的  
	// p2.PersonAddAge(p1); p2.PersonAddAge(p1); p2.PersonAddAge(p1);  相当于 p2 调用了 3 次 PersonAddAge(p1)
	
	// 链式编程思想
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);

	cout << "p2的年龄为:" << p2.age << endl;
}

int main()
{

	// test01();

	test02();

	system("pause");

	return 0;
}

4.3.3 空指针访问成员函数

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

	int m_Age;
};

void test01()
{
	Person* p = NULL;

	p->showClassName();

	p->showPersonAge();
}

int main()
{

	test01();

	system("pause");

	return 0;
}

4.3.4 const修饰成员函数

常函数:

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

常对象:

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

示例:

#include <iostream>
using namespace std;

// 常函数
class Person
{
public:

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

	void func()
	{
		m_A = 100;
	}

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

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

// 常对象

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

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

int main()
{



	system("pause");

	return 0;
}

原文链接: https://www.cnblogs.com/zhouxuzhi/p/15779856.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++ 类和对象--对象模型和this指针

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/184020

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 上午10:38
下一篇 2023年2月12日 上午10:39

相关推荐