【c++】C++对象模型和this指针

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

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

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

(1)空对象的大小

#include <iostream>
using namespace std;

class Person
{
};

void test01() {
	Person p1;
	cout << "空对象的大小是:" << sizeof(p1) << endl;
}

int main(void)
{
	test01();

	system("pause");
	return 0;
}

空对象占用内存空间为: 1 ;C++编译器会给每个空对象也分配一个字节空间,是为了区分空对象占内存的位置 ;

每任空对象也应该有一个独一-无二的内存地址 。

#include <iostream>
using namespace std;

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

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

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

	static void func2(){}  //静态成员函数  不属于类的对象上
};
int Person::m_B = 10;

void test01() {
	Person p1;
	cout << "p1的大小是:" << sizeof(p1) << endl;
}

int main(void)
{
	test01();

	system("pause");
	return 0;
}

2、this指针概念

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

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

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

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

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

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

this指针的用途:

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

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

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

不用this

#include <iostream>
using namespace std;

class Person2
{
public:
	Person2(int age)
	{
		age = age;     //编译器会认为这三个age是一样的
	}

	int age;
};

void test02()
{
	Person2 p(18);
	cout << p.age << endl;
}

int main(void)
{
	test02();

	system("pause");
	return 0;
}

使用this

#include <iostream>
using namespace std;

class Person2
{
public:
	Person2(int age)
	{
		this->age = age;    
	}

	int age;
};

void test02()
{
	Person2 p(18);
	cout << p.age << endl;
}

int main(void)
{
	test02();

	system("pause");
	return 0;
}

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

现在有个需求要一直给p2加p1的年龄

#include <iostream>
using namespace std;

class Person2
{
public:
	Person2(int age)
	{
		this->age = age;    
	}

	Person2& addAge(Person2& p)
	{
		this->age += p.age;
		return *this;
	}


	int age;
};

void test02()
{
	Person2 p1(10);

	Person2 p2(10);

	p2.addAge(p1).addAge(p1).addAge(p1);


	cout << p2.age << endl;
}

int main(void)
{
	test02();

	system("pause");
	return 0;
}

3、空指针访问成员函数

C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针

如果用到this指针,需要加以判断保证代码的健壮性

#include <iostream>
using namespace std;

class Person3
{
public:
	void showClass()
	{
		cout << "this is Person3" << endl;
	}

	void showAge()
	{
		cout << "age = " << age << endl;
	}

	int age;
};


void test03()
{
	//空指针访问成员函数
	Person3* p = NULL;
	p->showClass();    //空指针,可以调用成员函数

	p->showAge();      //但是如果成员函数中用到了this指针,就不可以了
}


int main(void)
{
	test03();

	system("pause");
	return 0;
}

修改代码如下:

#include <iostream>
using namespace std;

class Person3
{
public:
	void showClass()
	{
		cout << "this is Person3" << endl;
	}

	void showAge()
	{
		if (this == NULL)
		{
			return;
		}
		cout << "age = " << age << endl;
	}

	int age;
};


void test03()
{
	//空指针访问成员函数
	Person3* p = NULL;
	p->showClass();    //空指针,可以调用成员函数

	p->showAge();      //但是如果成员函数中用到了this指针,就不可以了
}


int main(void)
{
	test03();

	system("pause");
	return 0;
}

经测试,静态成员函数也是可以通过空指针访问的。

4、const修饰成员函数

(1)常函数

常函数:

  • 成员函数后加const后我们称为这个函数为常函数

  • 常函数内不可以修改成员属性

  • 成员属性声明时加关键字mutable后,在常函数中依然可以修改

#include <iostream>
using namespace std;

class Person4
{
public:


	//1、this指针的本质是一个指针常量,指针的指向不可修改
	//5、如果想让指针指向的值也不可以修改,需要声明常函数
	void show()const    //5
	{
		//3、const Type* const pointer;
		//2、this = NULL; //不能修改指针的指向 Person* const this;
		//4、this->m_A = 100; //但是this指针指向的对象的数据是可以修改的

		//6、const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
		this->m_B = 100;
	}
	int m_A;
	mutable int m_B; //可修改 可变的
};


int main(void)
{
	

	system("pause");
	return 0;
}
  • this指针的本质是一个指针常量,指针的指向不可修改
  • this = NULL; //不能修改指针的指向 Person* const this;
  • this->m_A = 100; //但是this指针指向的对象的数据是可以修改的
  • 如果想让指针指向的值也不可以修改,需要声明常函数;
  • void show()const   {}
  • const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量

(2)常对象

常对象:

  • 声明对象前加const称该对象为常对象

  • 常对象只能调用常函数

#include <iostream>
using namespace std;

class Person4
{
public:


	//1、this指针的本质是一个指针常量,指针的指向不可修改
	//5、如果想让指针指向的值也不可以修改,需要声明常函数
	void show()const    //5
	{
		//3、const Type* const pointer;
		//2、this = NULL; //不能修改指针的指向 Person* const this;
		//4、this->m_A = 100; //但是this指针指向的对象的数据是可以修改的

		//6、const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
		this->m_B = 100;
	}

	void MyFunc1() 
	{
		
	}

	void MyFunc2()const
	{

	}

	int m_A;
	mutable int m_B; //可修改 可变的
};


void test004()
{
	const Person4 person; //常量对象  
	cout << person.m_A << endl;
	//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问
	person.m_B = 100; //但是常对象可以修改mutable修饰成员变量


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


int main(void)
{
	test004();

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Zhouzi_heng/article/details/113702802