文章内容转载自黑马程序员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;//静态成员变量 不属于类对象上

//void func() {//加了函数 字节还是4 说明非静态成员函数不属于类对象上
//}

//static void func2() {//加了静态函数 字节还是4 说明静态成员函数不属于类对象上
//}

};
int Person2::m_b = 100;
void test01() {
//空对象占用的内存空间
//C++编译器会给每个空对象也分配一个字节空间,为了区分不同的空对象所占用的内存空间的位置
//每个空对象也应该有一个独一无二的内存地址
Person p;
cout << sizeof(p) << endl;
//输出为1

};

void test02() {

Person1 p1;
cout << sizeof(p1) << endl;
//输出为4,对应int m_a的字节,

Person2 p2;
cout << sizeof(p2) << endl;
//输出还是4,对应int m_a的字节
//说明static int m_b不属于类的对象上
}
int main() {
//test01()
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)
{
//1、当形参和成员变量同名时,可用this指针来区分
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) {//点击左边的age,可以发现编译器认为该行的age与下一行的age是同一个,与最下行的age不是同一个
// age = age;
//}
//
//int age;//这个age实际上从来都没有进行过赋值操作

//解决方法:
Person(int age) {
this->age = age;//this指向的是 被调用的成员函数 所属的对象
}

void PersonAddAge(Person& p) {
this->age += p.age;
}

Person& PersonAddAge1(Person& p) {
this->age += p.age;
//this指向p2的指针,而*this指向的是p2这个对象本体,注意这里需要用引用的方式返回本体
//如果是返回值的方式,输出结果是20
return *this;
}

int age;
};
//1、解决名称冲突
void test01() {
Person p1(19);//this指向的是p1
cout << p1.age << endl;
}
//2、返回对象本身用*this
void test02() {
Person p1(10);

//只加上1次
Person p2(10);

p2.PersonAddAge(p1);
cout << p2.age << endl;
//输出结果20
}

void test03() {
Person p1(10);

//加上多次
Person p2(10);

//p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);这样写会报错,因为前面的函数返回值是void
//如果可以使得p2.PersonAddAge(p1)返回的还是p2,依此类推,可以持续执行

//链式编程思想
p2.PersonAddAge1(p1).PersonAddAge1(p1).PersonAddAge1(p1);
//如果是返回值的方式,输出结果是20,返回的不是本体了,而是用本体创建一个新的数据p2',调用了拷贝构造函数,依此类推p2'',p2'''等等
cout << p2.age << endl;

}
int main(){
//test01();
//test02();
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(); //但是如果成员函数中用到了this指针,就不可以了
}

int main() {

test01();

system("pause");

return 0;
}

自己写的:

#include<iostream>
using namespace std;
class Person {
public:
void showClassName() {
cout << "Person Class" << endl;
}

void showPersonAge() {

//原因是传入的指针是NULL
cout << m_age << endl;//实际上在属性前面默认加上了this->,说明是当前对象的属性
}

void showPersonAge1() {
//解决方法
if (this == NULL) {
return;
}
//原因是传入的指针是NULL
cout << m_age << endl;//实际上在属性前面默认加上了this->,说明是当前对象的属性
}
int m_age = 0;
};
void test01() {
//老版本的结果是系统崩了,现在是没有输出
Person* p=NULL;//指针指向的Person是一个空指针,相当于this->是一个空的东西,没有实体,没有创建一个确定的对象,this根本就没有指向一个确定的数据

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;
}

//this指针的本质是一个指针常量,指针的指向不可修改
//如果想让指针指向的值也不可以修改,需要声明常函数
void ShowPerson() const {
//const Type* const pointer;
//this = NULL; //不能修改指针的指向 Person* const this;
//this->mA = 100; //但是this指针指向的对象的数据是可以修改的

//const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
this->m_B = 100;
}

void MyFunc() const {
//mA = 10000;
}

public:
int m_A;
mutable int m_B; //可修改 可变的
};


//const修饰对象 常对象
void test01() {

const Person person; //常量对象
cout << person.m_A << endl;
//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问
person.m_B = 100; //但是常对象可以修改mutable修饰成员变量

//常对象访问成员函数
person.MyFunc(); //常对象不能调用const的函数

}

int main() {

test01();

system("pause");

return 0;
}

自己写的:

#include<iostream>
using namespace std;
//常函数
class Person {
public:
//this指针的本质是一个指针常量 指针的指向是不可以修改的
//this实际上就是Person * const this
void showPerson0() {//不可以修改成员属性的值
this->m_a = 100; //实际上是this->m_a = 100;正常运行
//this=NULL;this指针不可以修改指针的指向
}

//this指针的本质是一个指针常量
//下面加上了const 这里就是const Person * const this 此时this也不可以修改指针的值了
//在成员函数后面加上const,修饰的是this指向,让指针的指向的值也不可以修改
void showPerson1() const{//不可以修改成员属性的值
this->m_b = 100;//正常,没有保错
//m_a = 100; 实际上是this->m_a = 100;执行时报错
//this=NULL;this指针不可以修改指针的指向
}

int m_a;

mutable int m_b;//特殊变量,即使在常函数中,也可以修改这个值,加上mutable关键字
};
void test01() {
Person p;
p.showPerson0();
}
//常对象
void test02() {
const Person p;//在对象前面加上const,变为常对象
//p.m_a = 100;会报错,不可以修改
p.m_b;//正常运行,m_b在常对象下也可以修改

//常对象只能调用常函数
p.showPerson1();//正常运行,没有报错
//p.showPerson0();//不可以调用,因为普通成员函数可以修改属性 否则的话说明常对象里面的数据也可以修改

}
int main() {
test01();
test02();
return 0;
}