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

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

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

#include<iostream>
#include<string>
using namespace std;
class h_idol{    
};

void call_idol()
{
	h_idol Mona;
	cout << sizeof(Mona) << endl; 
}

int main() {
	
	call_idol();

	system("pause");

	return 0;
}

输出:

1
请按任意键继续. . .

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

class h_idol{  
  //非静态成员变量占对象空间
	int idol;  
};

输出:

4
请按任意键继续. . .

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

class h_idol{  
//静态成员变量不占对象空间
	static int idol;  
};

输出:

1
请按任意键继续. . .

4.成员函数也不属于类的对象上

class h_idol{  
//成员函数也不占对象空间
	static void h_idols_01(){
	}
	void h_idols_02(){
	}
};

输出:

1
请按任意键继续. . .

1.2 this指针概念

问题的提出:

  • 每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
  • 那么问题是:这一块代码是如何区分那个对象调用自己的呢?
    解决:
  • c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象
  • this指针是隐含每一个非静态成员函数内的一种指针
  • this指针不需要定义,直接使用即可
    this的作用
  • 解决名称冲突 , 当形参和成员变量同名时,可用this指针来区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this
#include<iostream>
#include<string>
using namespace std;
class h_idol{  
public:
	//h_idol(string a): idols(a){
	//}
    h_idol(string idols)
    {
    //1、当形参和成员变量同名时,可用this指针来区分
       this->idols = idols;   
	h_idol& sumidols(h_idol &h){
	//this指向Mona01的指针,而*this指向的就是Mona01这个对象本体
		this->idols += h.idols;
	}
	string  idols;
};

void call_idol()
{
	h_idol Mona00("H");
	h_idol Mona01("H");
    Mona01.sumidols(Mona00).sumidols(Mona00).sumidols(Mona00).sumidols(Mona00);
    cout << Mona01.idols << endl;
}

int main() {
	
	call_idol();

	system("pause");

	return 0;
}

输出:

HHHHH
请按任意键继续. . .
  • 如果返回的时候是以的方式,他一定创建的是一个新的对象,如果是引用的方式返回,他一定返回的是原来的对象。
h_idol sumidols(h_idol &h){
		this->idols += h.idols;
		return * this;
	}

输出:

HH
请按任意键继续. . .

1.3 空指针访问成员函数

C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针
如果用到this指针,需要判断加以判断保证代码的健壮性

//空指针访问成员函数
class h_idol{  
public:
//	h_idol(string a): idols(a){
//	}
    void idol00()
    {
    	cout << "道枝骏佑" << endl; 
	}

	string idols ;
	void idol01(){
		if (this == NULL){
			return;
		}
		idols = "道枝骏佑"; 
	cout << idols << endl;//相等于cout <<this->idols << endl;	
	}

};
void call_idol()
{
	h_idol *Mona00 = NULL;
	//空指针,可以调用成员函数
	Mona00->idol00();
	 //但是如果成员函数中用到了this指针,就不可以了
	Mona00->idol01();
	Mona->idol;
}
  • this指针的本质是一个指针常量,指针的指向不可修改
    this相当于 h_idol* const this;
void idol00() 
    {
    	this->idols = "薄荷双生"; //this指针指向的对象的数据是可以修改的
    	this->NULL;	//但是不能修改指针的指向 Person* const this;
    	cout << "道枝骏佑" << endl; 
	}
//错误提示:[Error] expected unqualified-id before '__null'
  • 如果想让指针指向的值在函数体内也不可以修改,需要声明常函数
  • const相当于修饰的是this指针相当于//const h_idol* const Mona;
  • const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
void idol00() const
    {
        idols = "薄荷双生"; 
    	cout << idols << endl; 
	}
	h_idol Mona00;
	Mona00.idol00();
//错误提示:[Error] passing 'const string {aka const std::basic_string<char>}' as 'this' argument of 'std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' discards qualifiers [-fpermissive]

注意1: 声明本函数为常量函数,指针指向的值在本函数体内也不可以修改,但可以在其他函数体内修改

void idol00() const
    {
//       idols = "薄荷双生"; 
    	cout << idols << endl; 
	}

	string idols = "道枝骏佑";
	void idol01(){
//		if (this == NULL){
//			return;
//		}
		idols = "黑魔法手帖"; 
	cout << idols << endl;//相等于cout <<this->idols << endl;	
	}

};
h_idol Mona00;
	Mona00.idol00();
	Mona00.idol01();

输出:

道枝骏佑
黑魔法手帖
请按任意键继续. . .

注意2: mutable修饰的变量可以修改

mutable string idols = "道枝骏佑";

	void idol01() const{
//		if (this == NULL){
//			return;
//		}
		idols = "黑魔法手帖"; 
	cout << idols << endl;//相等于cout <<this->idols << endl;	
	}

输出:

道枝骏佑
黑魔法手帖
请按任意键继续. . .
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值