1,成员变量和成员函数分开存储

在C++中,类内的成员变量和成员函数分开存储
只有非静态成员变量才属于类的对象上

  1. #include <iostream>
  2. using namespace std;
  3. class Person {
  4. public:
  5. Person() {
  6. mA = 0;
  7. }
  8. //非静态成员变量占对象空间
  9. int mA;
  10. //静态成员变量不占对象空间
  11. static int mB;
  12. //函数也不占对象空间,所有函数共享一个函数实例
  13. void func() {
  14. cout << "mA:" << this->mA << endl;
  15. }
  16. //静态成员函数也不占对象空间
  17. static void sfunc() {
  18. }
  19. };
  20. int main() {
  21. cout << sizeof(Person) << endl;
  22. /*
  23. 空对象占用空间大小为:1
  24. C++编译器会给每个空对象也分配一个字节的空间,是为了区分空对象占内存的位置
  25. 每个空对象也应该有一个独一无二的内存地址
  26. */
  27. system("pause");
  28. return 0;
  29. }

2,this指针概念

每一个非静态成员函数只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码
那么问题是:这一块代码是如何区分那个对象调用自己的呢?
c++通过提供特殊的对象指针,this指针,解决上述问题。this指针指向被调用的成员函数所属的对象
this指针是隐含每一个非静态成员函数内的一种指针
this指针不需要定义,直接使用即可
this指针的用途:

  • 当形参和成员变量同名时,可用this指针来区分
  • 在类的非静态成员函数中返回对象本身,可使用return *this


  1. #include <iostream>
  2. using namespace std;
  3. class Person
  4. {
  5. public:
  6. Person(int age)
  7. {
  8. //1、当形参和成员变量同名时,可用this指针来区分
  9. //this指针指向被调用的成员函数所属的对象
  10. this->age = age;
  11. }
  12. Person& PersonAddPerson(Person p)
  13. {
  14. this->age += p.age;
  15. //返回对象本身
  16. return *this;
  17. }
  18. int age;
  19. };
  20. void test01()
  21. {
  22. Person p1(10);
  23. cout << "p1.age = " << p1.age << endl;
  24. Person p2(10);
  25. p2.PersonAddPerson(p1).PersonAddPerson(p1).PersonAddPerson(p1);
  26. cout << "p2.age = " << p2.age << endl;
  27. }
  28. int main() {
  29. test01();
  30. system("pause");
  31. return 0;
  32. }

3,空指针访问成员函数

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

  1. #include <iostream>
  2. using namespace std;
  3. // 空指针访问成员函数
  4. class Person {
  5. public:
  6. void ShowClassName() {
  7. cout << "我是Person类!" << endl;
  8. }
  9. void ShowPerson() {
  10. if (this == NULL) {
  11. return;
  12. }
  13. cout << mAge << endl;//这里的mAge实际上是this->mAge
  14. }
  15. public:
  16. int mAge;
  17. };
  18. void test01()
  19. {
  20. Person* p = NULL;
  21. p->ShowClassName(); //空指针,可以调用成员函数
  22. p->ShowPerson(); //但是如果成员函数中用到了this指针,就不可以了
  23. }
  24. int main() {
  25. test01();
  26. system("pause");
  27. return 0;
  28. }

4,const修饰成员函数

常函数:

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

常对象:

  • 声明对象前加const称该对象为常对象
  • 常对象只能调用常函数

代码示例

  1. #include <iostream>
  2. using namespace std;
  3. class Person {
  4. public:
  5. Person() {
  6. m_A = 0;
  7. m_B = 0;
  8. }
  9. //this指针的本质是一个指针常量,指针的指向不可修改
  10. //如果想让指针指向的值也不可以修改,需要声明常函数
  11. void ShowPerson() const {
  12. //const Type* const pointer;
  13. //this = NULL; //不能修改指针的指向 Person* const this;
  14. //this->mA = 100; //但是this指针指向的对象的数据是可以修改的
  15. //const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
  16. this->m_B = 100;
  17. }
  18. void MyFunc() const {
  19. //mA = 10000;
  20. }
  21. public:
  22. int m_A;
  23. mutable int m_B; //可修改 可变的
  24. };
  25. //const修饰对象 常对象
  26. void test01() {
  27. const Person person; //常量对象
  28. cout << person.m_A << endl;
  29. //person.mA = 100; //常对象不能修改成员变量的值,但是可以访问
  30. person.m_B = 100; //但是常对象可以修改mutable修饰成员变量
  31. //常对象访问成员函数
  32. person.MyFunc(); //常对象不能调用const的函数
  33. }
  34. int main() {
  35. test01();
  36. system("pause");
  37. return 0;
  38. }