文章内容转载自黑马程序员C++核心编程讲义,如有侵权,请联系作者删除
 
4.3 C++对象模型和this指针 
4.3.1
成员变量和成员函数分开存储 
在C++中,类内的成员变量和成员函数分开存储 
只有非静态成员变量才属于类的对象上 
(静态成员变量和静态成员函数不属于某个对象,非静态成员函数不属于类的对象(非静态成员函数只有一份) )
class  Person  {public :	Person () { 		mA = 0 ; 	} 	 	int  mA; 	 	static  int  mB;  	 	void  func ()   { 		cout << "mA:"  << this ->mA << endl; 	} 	 	static  void  sfunc ()   { 	} }; int  main ()   {	cout << sizeof (Person) << endl; 	system ("pause" ); 	return  0 ; } 
 
自己写的:
#include <iostream>  using  namespace  std;class  Person  {}; class  Person1  {	int  m_a; }; class  Person2  {	int  m_a; 	 	static  int  m_b; 	 	 	 	 }; int  Person2::m_b = 100 ;void  test01 ()   {	 	 	 	Person p; 	cout << sizeof (p) << endl;      }; void  test02 ()   {	Person1 p1; 	cout << sizeof (p1) << endl; 	 	Person2 p2; 	cout << sizeof (p2) << endl; 	 	 } int  main ()   {	 	test02 (); 	return  0 ; } 
 
4.3.2 this指针概念 
通过4.3.1我们知道在C++中成员变量和成员函数是分开存储的
每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
那么问题是:这一块代码是如何区分那个对象调用自己的呢?
c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象 
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可
this指针的用途:
当形参和成员变量同名时,可用this指针来区分  
**在类的非静态成员函数中返回对象本身,可使用return *this** 
 
class  Person { public :	Person (int  age) 	{ 		 		this ->age = age; 	} 	Person& PersonAddPerson (Person p)   	 {		this ->age += p.age; 		 		return  *this ; 	} 	int  age; }; void  test01 ()  {	Person p1 (10 )  ; 	cout << "p1.age = "  << p1.age << endl; 	Person p2 (10 )  ; 	p2.PersonAddPerson (p1).PersonAddPerson (p1).PersonAddPerson (p1); 	cout << "p2.age = "  << p2.age << endl; } int  main ()   {	test01 (); 	system ("pause" ); 	return  0 ; } 
 
自己写的:
#include <iostream>  using  namespace  std;class  Person  {public :	 	 	 	 	 	 	Person (int  age) { 		this ->age = age; 	} 	 	void  PersonAddAge (Person& p)   { 		this ->age += p.age; 	} 	Person& PersonAddAge1 (Person& p)   { 		this ->age += p.age; 		 		 		return  *this ; 	} 	int  age; }; void  test01 ()   {	Person p1 (19 )  ; 	cout << p1.age << endl; } void  test02 ()   {	Person p1 (10 )  ;      	 	Person p2 (10 )  ; 	p2.PersonAddAge (p1); 	cout << p2.age << endl; 	 } void  test03 ()   {	Person p1 (10 )  ; 	 	Person p2 (10 )  ; 	 	 	 	p2.PersonAddAge1 (p1).PersonAddAge1 (p1).PersonAddAge1 (p1); 	 	cout << p2.age << endl; 	 } int  main ()  {	 	 	test03 (); 	return  0 ; } 
 
4.3.3 空指针访问成员函数 
C++中空指针也是可以调用成员函数的,但是也要注意有没有用到this指针 
如果用到this指针,需要加以判断保证代码的健壮性 
示例: 
class  Person  {public :	void  ShowClassName ()   { 		cout << "我是Person类!"  << endl; 	} 	void  ShowPerson ()   { 		if  (this  == NULL ) { 			return ; 		} 		cout << mAge << endl; 	} public :	int  mAge; }; void  test01 ()  {	Person * p = NULL ; 	p->ShowClassName ();  	p->ShowPerson ();   } int  main ()   {	test01 (); 	system ("pause" ); 	return  0 ; } 
 
自己写的:
#include <iostream>  using  namespace  std;class  Person  {public :	void  showClassName ()   { 		cout << "Person Class"  << endl; 	} 	void  showPersonAge ()   { 		 		cout << m_age << endl; 	} 	void  showPersonAge1 ()   { 		 		if  (this  == NULL ) { 			return ; 		} 		 		cout << m_age << endl; 	} 	int  m_age = 0 ; }; void  test01 ()   {	 	Person* p=NULL ; 	p->showClassName (); 	p->showPersonAge (); 	p->showPersonAge1 (); } int  main ()   {	test01 (); 	return  0 ; } 
 
4.3.4 const修饰成员函数 
常函数: 
成员函数后加const后我们称为这个函数为常函数  
常函数内不可以修改成员属性  
成员属性声明时加关键字mutable后,在常函数中依然可以修改  
 
常对象: 
声明对象前加const称该对象为常对象  
常对象只能调用常函数  
 
示例: 
class  Person  {public :	Person () { 		m_A = 0 ; 		m_B = 0 ; 	} 	 	 	void  ShowPerson ()  const   { 		 		 		 		 		this ->m_B = 100 ; 	} 	void  MyFunc ()  const   { 		 	} public :	int  m_A; 	mutable  int  m_B;  }; void  test01 ()   {	const  Person person;  	cout << person.m_A << endl; 	 	person.m_B = 100 ;  	 	person.MyFunc ();  } int  main ()   {	test01 (); 	system ("pause" ); 	return  0 ; } 
 
自己写的:
#include <iostream>  using  namespace  std;class  Person  {public :	 	 	void  showPerson0 ()   { 		this ->m_a = 100 ;  		 	} 	 	 	 	void  showPerson1 ()  const  { 		this ->m_b = 100 ; 		 		 	} 	int  m_a; 	mutable  int  m_b; }; void  test01 ()   {	Person p; 	p.showPerson0 (); } void  test02 ()   {	const  Person p; 	 	p.m_b; 	 	p.showPerson1 (); 	 } int  main ()   {	test01 (); 	test02 (); 	return  0 ; }