C++类与对象3——对象模型与this指针

1. 成员变量和成员函数分开存储

成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上
#include <iostream>
using namespace std;
/*成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上*/

class Person
{
    
    
	int m_A; //非静态成员变量 属于类的对象上
	
	static int m_B;// 静态成员变量 不属于类的对象上
	void func() {
    
    } //非静态成员函数 不属于类的对象上
	static void func2() {
    
    } //静态成员函数 不属于类的对象上
};
int Person::m_B = 0;
void test01()
{
    
    
	Person p;
	//空对象占用内存空间为:1
	//解释:c++编译器会给每个空对象也分配一个字节的空间,是为了区分空对象占内存的位置
	//每个空对象也应该有一个独一无二的内存地址
	cout << "size of p = " << sizeof(p) << endl; // 运行结果是1
}
void test02()
{
    
    
	Person p;
	cout << "size of p = " << sizeof(p) << endl;
}
int main()
{
    
    
	//test01();
	test02();
	system("pause");
	return 0;
}

2. this指针概念

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

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

this指针的用途:
  	当形参和成员变量同名时,可用this指针来区分
  	在类的非静态成员函数中返回对象本身,可使用return *this*/
1.解决名称冲突
2.返回对象本身用*this
#include <iostream>
using namespace std;

class Person
{
    
    
public:
	Person(int age) //鼠标放在这里时,分析int age的age和其他3个age不是同一个age
	{
    
    
		//this 指针指向的是 被调用的成员函数 所属的对象
		//当形参和成员变量同名时,可用this指针来区分
		//age = age;
		 this->age = age; //第二种方法
	}

	Person& PersonAddAge(Person &p)
	{
    
    
		this->age += p.age;

		//this指向的是p2的本体,而*this指向的就是p2这个对象的本体
		return *this;
	}

	//int m_age; //m代表成员函数 第一种方法
	int age;
};

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

//2.返回对象本身用*this
void test02()
{
    
    
	Person p1(10);
	Person p2(10);

	//链式编程思想
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
	cout << "p2的年龄:" << p2.age << endl;
}
int main()
{
    
    
	//test01();
	test02();
	system("pause");
	return 0;
}

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=" << m_age << endl;
	}
	int m_age;
};

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

4.const修饰成员函数

const修饰成员函数
常函数:
    	成员函数后加const后我们称这个函数为常函数
		常函数内不可以修改成员属性
		成员属性声明时加关键字mutable后,在常函数中依然可以修改
	
常对象:
    	声明对象前加const称该对象为常对象
		常对象只能调用常函数
#include <iostream>
using namespace std;

//常函数
class Person
{
    
    
public:

	//this本质是  指针常量  指针的指向是不可以修改的
	// const Person * const this
	//在成员函数变量后加const,修饰的是this指针,让指针指向的值也不可以修改,
	void showPerson() const
	{
    
    
		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 = 100; 不可以改
	p.m_B = 100; //m_B在常对象下也可以修改

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

猜你喜欢

转载自blog.csdn.net/qq_45923342/article/details/105824641
今日推荐