【学习笔记】C++ 核心编程(七)类和对象——C++对象模型 + this指针内容来自小破站《黑马程序员C++》复习自用
- 4.3 C++对象模型和this指针
- 4.3.1 成员变量和成员函数分开存储
- 4.3.2 this指针的概念
- 4.3.3 空指针访问成员函数
- 4.3.4 const修饰成员函数
- 4.4 友元、运算符重载、继承、多态、文件操作(见后续更新…)
在C++中,类内的成员变量和成员函数分开存储
只有非静态成员变量才属于累的对象上
现在是一个空类,看看有多大
#includeusing namespace std; //成员变量 和 成员函数 是分开存储的 class Person//现在是一个空类,看看有多大 { }; void test01() { Person p; cout << "size of p = " << sizeof(p) << endl << endl; } int main() { test01(); system("pause"); return 0; }
一个字节。C++编译器会给每个空对象也分配一个字节的空间,是为了区分空对象占内存的位置。
每一个空对象,都要不同,都有独一无二的内存空间
静态成员变量:
#includeusing namespace std; //成员变量 和 成员函数 是分开存储的 class Person//现在是一个空类,看看有多大 { }; class Person2 { int m_A; //非静态成员变量(没有static)属于类的对象上 }; class Person3 { static int m_B; //静态成员变量,类内声明类外初始化,不属于类对象上 }; int Person3::m_B = 0; void test01() { Person p1; cout << endl << "size of p1 = " << sizeof(p1) << endl << endl; } void test02() { Person2 p2; cout << endl << "size of p2 = " << sizeof(p2) << endl << endl; } void test03() { Person3 p3; cout << endl << "size of p3 = " << sizeof(p3) << endl << endl; } int main() { test01(); test02(); test03(); system("pause"); return 0; }
非静态成员函数:不属于类的对象上
#includeusing namespace std; //成员变量 和 成员函数 是分开存储的 class Person//现在是一个空类,看看有多大 { }; class Person2 { int m_A; //非静态成员变量(没有static)属于类的对象上 }; class Person3 { static int m_B; //静态成员变量,类内声明类外初始化,不属于类对象上 }; int Person3::m_B = 0; class Person4 { void fun(){}//非静态成员函数(没有static)不属于类的对象上 //静态成员函数,也不属于类的对象上 }; void test01() { Person p1; cout << endl << "size of p1 = " << sizeof(p1) << endl << endl; } void test02() { Person2 p2; cout << endl << "size of p2 = " << sizeof(p2) << endl << endl; } void test03() { Person3 p3; cout << endl << "size of p3 = " << sizeof(p3) << endl << endl; } void test04() { Person4 p4; cout << endl << "size of p4 = " << sizeof(p4) << endl << endl; } int main() { test01(); test02(); test03(); test04(); system("pause"); return 0; }
通过4.3.1我们知道在C++中成员变量和成员函数是分开存储的
每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码那么问题是:这—块代码是如何区分那个对象调用自己的呢?
C++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象
谁调用这块内存,this就指向谁。
this指针的特点:
- this指针是隐含每一个非静态成员函数内的一种指针
- this指针不需要定义,直接使用即可
this指针的用途:
- 当形参和成员变是同名时,可用this指针来区分
- 在类的非静态成员函数中返回对象本身,可使用return *this
一. 解决名称冲突
#includeusing namespace std; class Person { public: Person(int age) { age = age; } int age; }; // 1. 解决名称冲突 void test01() { Person p1(18); cout << "The age of p1 is " << p1.age << endl; } // 2. 返回对象本身用 *this int main() { test01(); system("pause"); return 0; }
是因为编译器认为这三个age是一回事,实则不是。
方法一
方法二
this指针,指向的是,被调用的成员函数所属的对象
现在这个函数,是p1在调,this就指向p1
二. 返回对象本身
首先,想实现一个年龄相加的操作
#includeusing namespace std; class Person { public: Person(int age) { this->age = age; } void PersonAddAge(Person& p) { this->age += p.age; } int age; }; // 1. 解决名称冲突 void test01() { Person p1(18); cout << "The age of p1 is " << p1.age << endl << endl; } // 2. 返回对象本身用 *this void test02() { Person p1(10); Person p2(10); p2.PersonAddAge(p1); cout << "the age of p2 is " << p2.age << endl << endl; } int main() { test01(); test02(); system("pause"); return 0; }
成功,但是仅仅加一次,没加爽还想加,是否可以利用
p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);
不可以,因为现在的返回值p2.PersonAddAge(p1)的返回值是void,就不能再次调用了。
但是如果函数调用了一次之后,返回的依然是p2,就可以再继续.PersonAddAge(p1)了。
当p2来调用这个函数的时候,this指向p2,那么如果return一个* this,也就是p2对象的本体。
如果写了*this,就不能用void了,需要换成Person&,要使用引用的方式做一个返回。
#includeusing namespace std; class Person { public: Person(int age) { this->age = age; } Person& PersonAddAge(Person& p) { this->age += p.age; return *this; } int age; }; // 1. 解决名称冲突 void test01() { Person p1(18); cout << "The age of p1 is " << p1.age << endl << endl; } // 2. 返回对象本身用 *this void test02() { Person p1(10); Person p2(10); p2.PersonAddAge(p1); cout << "the age of p2 is " << p2.age << endl << endl; } int main() { test01(); test02(); system("pause"); return 0; }
以上思想:【链式编程思想】
另一方面,如果返回的不是引用,是一个指,只写一个Person,还能正常输出吗?
第一次调用之后,p2+10,返回的不是p2的本体了。返回的是20,是一个形成的新的数据。即:
C++中,指针也是可以调用成员函数的,但是也要注意有没有用到this指针,如果用到了this指针,需要加以判断保证代码的健壮性。
#includeusing namespace std; class Person { public: void showClassName() { cout << "this is Person class" << endl; } void showPersonAge() { cout << "age = " << m_Age << endl; } int m_Age; }; void test01() { Person* p = NULL; p->showClassName(); p->showPersonAge(); } int main() { test01(); system("pause"); return 0; }
原因分析:
第二个用到了m_Age属性,在默认情况下,属性前都默认成;
cout << "age = " << this->m_Age << endl;
表示的意思是,这是当前对象的一个属性,但是指针指向的Person是一个空指针,this根本没有指向一个确切的值,是一个空的值,还想访问属性,是不可行的。无中生有。
一些教材的解决方案
void showPersonAge()
{
//报错原因是因为传入的指针是NULL
if (this == NULL)
{
return;
}
cout << "age = " << this->m_Age << endl;
}
代码直接返回,如果为空,也不崩。
4.3.4 const修饰成员函数常函数:
- 成员函数后加const后我们称为这个函数为常函数
- 常函数内不可以修改成员属性
- 成员属性声明时加关键字mutable后,在常函数中依然可以修改
常对象:
- 声明对象前加const称该对象为常对象
- 常对象只能调用常函数
在函数的内部,不管传不传,都有一个隐含的this指针,相当于有一个this->在修饰着m_A,即
this->m_A = 100;
而this指针的本质是一个指针常量,也就是指针的指向是不可修改的。但是this指针指向的值是可以修改的。
在成员函后边加了一个const,本质修饰的是this指针,使得指针指向的值也不可以修改
最后…



