2021-8-3 C++进阶学习 -> 4.3 C++对象模型和this指针

目录

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

4.3.2 this指针概念

4.3.3 空指针访问成员函数

4.3.4 const修饰成员函数


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

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

    int a;//非静态成员变量,属于类的对象上的

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

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

空对象占用的内存空间为 1
   c++编译器也会给每个空对象分配一个字节空间,是为了区分空对象占内存的位置
   每个空对象也应该有一个独一无二的内存地址

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

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

class person{

	int a;//非静态成员变量,属于类的对象上的

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

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

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

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

void text02()
{
	person p;
	cout << "size of p:" << sizeof(p) << endl;
}
int main() {

	text01();
	text02();
	system("pause");
}

4.3.2 this指针概念

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

1、解决名称冲突

2、返回对象本身用*this

#include <iostream>
#include <string>
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 text01()
{
	person p1(18);
	cout << "p1的年龄为:" << p1.age << endl;
}

//2、返回对象本身用*this
void text02()
{
	person p1(10);

	person p2(10);

	//链式编程思想
	p2.personaddage(p1).personaddage(p1).personaddage(p1);
	cout << "p2的年龄为:" << p2.age << endl;
}
int main() {

	text01();
	text02();
	system("pause");
}

4.3.3 空指针访问成员函数

空指针调用成员函数:报错原因:传入的指针为null

if (this == NULL)
        {
            return;
        }//使用这段代码防止传入空指针时报错

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

//空指针调用成员函数

class person
{
public:

	void showclassname()
	{
		cout << "this is person class" << endl;
	}

	void showpersonage()
	{
		//报错原因:传入的指针为null

		if (this == NULL)
		{
			return;
		}
		cout << "age = " << this->age << endl;
	}
	int age;
};

void text01()
{
	person * p = NULL;

	p->showclassname();

	p->showpersonage();
}
int main() {

	text01();
	system("pause");
}

4.3.4 const修饰成员函数

this指针的本质  是指针常量  指针的指向是不可以修改的

const person *const this

在成员函数后面加const,修饰的是this指针,让指针指向的值也不可以修改

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

常对象    不可以调用普通成员函数 因为普通成员函数可以修改属性        常对象只能调用常函数

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

//常函数
class person
{
public:

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

	void func() {

	}
	int m_A;
	mutable int m_B;//特殊变量,即使在常函数中,也可以修改这个值
};
void text01()
{
	person p;
	p.showPerson();
}

//常对象

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

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


	system("pause");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值