(C++)类与对象——对象特性——C++对象模型和this指针

一、成员变量与成员函数分开储存

1.空对象所占字节为1

2.只有非静态成员变量占字节空间

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

class Person
{

public:
	int m_B;				//非静态成员 , 属于类对象上

	static int m_A;			//静态成员 , 不属于类对象上

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

	}

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

	}
};
//静态成员变量初始化
int Person::m_A = 100;
void test01()
{	
	Person p;
	//空对象占用内存空间为:1
	cout << sizeof(p) << endl;
	
}
void test02()
{
	Person p;
	cout << sizeof(p) << endl;

}
int main()
{	
	test02();
	system ("pause");
	return 0 ;
}

二、this指针的用途

1. 当形参与成员变量同名时,可以用来区分

2.在类的非静态成员函数中需要返回本身时,可使用return *this


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

class Person
{

public:
	
	int age;
	
	Person (int age)
	{
		this -> age = age;
		//this 指针指向的是 被调用的成员函数 所属的对象
	}
	Person& PersonAddAge(Person &p)
	{
		this -> age += p.age;
		//自身年龄 + 传入的对象的年龄
		return *this;
	}
	//若返回值是Person而不是Person&,则最终值不是40而是20
	//因为返回值的话,系统会创建一个新的对象,函数实现的功能是在新对象上
	//而不是在原本的对象上
};
void test01()
{	
	Person p(18);

	cout << p.age<< endl;
	
}
void test02()
{
	Person p1(10);
	Person p2(10);

	//链式编程思想
	p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
	//返回值是void  不能重复使用
	//返回值是Person& 的时候  可以使用
	cout << p2.age <<endl;
}
int main()
{	
	test02();
	system ("pause");
	return 0 ;
}

三、空指针调用成员函数

空指针可以调用成员函数,但也要注意有没有用到this指针,若是用到this指针,要加入if判断以加强代码的 健壮性


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

class Person
{

public:
	void showClassName()
	{
		cout << "this is class person" << 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();
	system ("pause");
	return 0 ;
}

四、const修饰成员函数

常函数:

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

常对象:

· 声明对象前加 const
· 常对象只能调用常函数

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

class Person
{
public:
	// this 指针的本质是 指针常量 指针的指向是不可修改的
	// 相当于 Person *const this
	// 在成员函数后面加const, 修饰的是this指向, 让指针指向的值也不可以修改
	//		  const Person *const this
	void showPerson() const
	{
		this -> m_B = 100;
		//this -> m_A = 100;
		//this = NULL 
		//this 指针不可以修改指针的指向
	}
	int m_A;
	mutable int m_B;
	//特殊变量, 使得该值在常函数中也可以修改
	void func()
	{
		m_A = 100;
		//在这里面修改不会报错
		//但调用时会报错
	}
};
void test01()
{	
	Person p;
	p.showPerson();
}
void test02()
{
	const Person p;
	p.m_A = 100;
	p.m_B = 100;
	//A不可修改
	p.showPerson();
	//常对象只能调用常函数
	p.func();
	//常对象不能调用普通函数,因为普通函数中可以修改常对象的值
	
}
int main()
{	
	test01();
	system ("pause");
	return 0 ;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值