【C++核心编程 4】——类和对象——4.3 C++对象模型和this指针[ 成员变量与成员函数 / this指针(自引用指针) / 空指针 / const修饰成员函数(常对象、常函数) ]

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


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

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

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


示例

空对象占用内存空间:1 个字节

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

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

#include<iostream>
using namespace std;
class Person {

};
void test01() {
	Person p;
	cout << "the size of P:" << sizeof(p) << endl;
	//空对象占用内存空间:1 个字节
}
int main() {
	test01();
	return 0;
}

非静态成员变量,属于类的对象上

占用内存空间:4 个字节

#include<iostream>
using namespace std;
class Person {
	int m_a;//非静态成员变量,属于类的对象上
};
void test01() {
	Person p;
	cout << "the size of P:" << sizeof(p) << endl;
	//一个非静态成员变量占用内存空间:4 个字节
}
int main() {
	test01();
	return 0;
}

静态成员变量,不属于类的对象上

不占用该内存空间

#include<iostream>
using namespace std;
class Person {
	static int m_b;//静态成员变量,不属于类的对象上,不占用内存长度
};
int Person::m_b=0;
void test01() {
	Person p;
	cout << "the size of P:" << sizeof(p) << endl;//1 空对象的长度
	
}
int main() {
	test01();
	return 0;
}

非静态成员函数,不属于类对象上,不占用该内存空间

静态成员函数,也不属于类对象上,不占用该内存空间

#include<iostream>
using namespace std;
class Person {
  //非静态成员函数
	void func() {

	}
  //静态成员函数
  static void func() {

	}
};
void test01() {
	Person p;
	cout << "the size of P:" << sizeof(p) << endl;//1 空对象的长度
	
}
int main() {
	test01();
	return 0;
}

4.3.2 this指针概念 (自引用指针)

通过上一节,我们知道在C++中成员变量和成员函数是分开存储的。
每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码。
那么:这一块代码是如何区分哪个对象调用自己的?

C++通过提供特殊的对象指针,this指针,解决上述问题。


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

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

this指针的用途:

  • 1,当形参和成员变量同名时,可以用this指针来区分
  • 2,在类的非静态成员函数中返回对象本身,可使用return *this

1, 当形参和成员变量同名时,可以用this指针来区分 ,即解决名称冲突

#include<iostream>
using namespace std;
class Person {
public:
	Person(int age) {
		this->age = age;
		//this指针指向 被调用的成员函数 所属的对象,此时this指针指向 p1
	}
	int age;
};
//1.解决名称冲突
void test01() {
	Person p1(20);
	cout << "p1的年龄:" << p1.age << endl;
}
int main() {
	test01();
	return 0;
}

2,返回对象本身用 *this

#include<iostream>
using namespace std;
class Person {
public:
	Person(int age) {
		this->age = age;
		//this指针指向 被调用的成员函数 所属的对象,此时this指针指向 p1
	}

	//若要返回本体,需要用引用的方式作为返回,用引用的方式返回  不会创建新的对象
	Person& Add_age(Person &p) {
		this->age += p.age;
		return *this;//返回p2
	}
	int age;
};

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

  //链式编程思想
	p2.Add_age(p1).Add_age(p1).Add_age(p1);
	cout << "p2的年龄:" << p2.age << endl;//40

}
int main() {
	test02();
	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() {
      //进行判断,增加代码的健壮性
		if (this == NULL) {
			return;
		}
		//报错原因是 传入的指针为NULL
		cout << "age=" << this->m_age << endl;
	}
	int m_age;
};
void test01() {
	Person* p = NULL;
	p->showClassName();//正常运行
	p->showPersonAge();//没有进行判断时会报错,读取访问权限冲突
}
int main() {
	test01();
	return 0;
}

4.3.4 const修饰成员函数 (常对象,常函数)

常函数:

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

示例:

#include<iostream>
using namespace std;
class Person {
public:
  //普通函数
	void showPerson() {
		//this指针的本质 是指针常量,指针的指向是不可修改的,但指针指向的值是可以修改,相当于 Person * const this 
		this->m_a = 100;
		//this=NULL;此行报错,因为 this指针不可修改指针的指向,原本指向 对象 p
	}
  
  //常函数
	void showPerson1() const{
		//m_a = 100; 常函数内不可修改成员变量的值

		//在成员函数后面加 const,修饰的是this指针,让指针指向的值也不可修改,const Person * const this 
		//this->m_a = 100;此行报错,指针指向的值也不可修改
		//this = NULL;此行报错,指针的指向不可修改

		this->m_b = 100;
	}
	int m_a;
	mutable int m_b;//特殊变量,加关键字mutable 即使在常函数中,也可以修改这个值
};
void test01() {
	Person p;
	p.showPerson();
}
int main() {
	test01();
	return 0;
}

常对象:

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

示例:

#include<iostream>
using namespace std;
class Person {
public:
   //普通函数
	void showPerson() {
		this->m_a = 100;
	}
  
  //常函数
	void showPerson1() const{
		this->m_b = 100;
	}
	int m_a;
	mutable int m_b;
};
void test02() {
	const Person p;
	//p.m_a = 100;此行报错,不可以修改m_a值
	p.m_b = 100;//m_b是特殊变量,有mutable关键字,在常对象下也可修改

	//常对象只能调用常函数,不能调用普通函数,因为普通成员函数可以修改属性
	//p.showPerson();此行报错
	p.showPerson1();
}
int main() {
	test02();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值