成员变量和函数的存储

c++实现了封装,数据和处理数据的操作(函数)是分开存储的。

  • c++中的非静态数据成员直接内含在类对象中,就像c struct一样。
  • 成员函数(member function)虽然内含在class声明之内,却不出现在对象中。
  • 每一个非内联成员函数(non-inline member function)只会诞生一份函数实例。 ```cpp class MyClass01{ public: int mA; };

class MyClass02{ public: int mA; static int sB; };

class MyClass03{ public: void printMyClass(){ cout << “hello world!” << endl; } public: int mA; static int sB; };

class MyClass04{ public: void printMyClass(){ cout << “hello world!” << endl; } static void ShowMyClass(){ cout << “hello world!” << endl; } public: int mA; static int sB; };

int main(){

  1. MyClass01 mclass01;
  2. MyClass02 mclass02;
  3. MyClass03 mclass03;
  4. MyClass04 mclass04;
  5. cout << "MyClass01:" << sizeof(mclass01) << endl; //4
  6. //静态数据成员并不保存在类对象中
  7. cout << "MyClass02:" << sizeof(mclass02) << endl; //4
  8. //非静态成员函数不保存在类对象中
  9. cout << "MyClass03:" << sizeof(mclass03) << endl; //4
  10. //静态成员函数也不保存在类对象中
  11. cout << "MyClass04:" << sizeof(mclass04) << endl; //4
  12. return EXIT_SUCCESS;

}

  1. <a name="Xk6LB"></a>
  2. ## this指针
  3. <a name="tKSkW"></a>
  4. ### this指针原理
  5. c++的数据和操作也是分开存储,并且每一个非内联成员函数(non-inline member function)只会诞生一份函数实例,也就是说多个同类型的对象会共用一块代码<br />那么问题是:这一块代码是如何区分那个对象调用自己的呢?<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21972649/1654161642946-232a07d0-0c54-44d6-92ff-4b663eeddab6.png#clientId=u1d27943c-c36d-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=185&id=u2927eeb0&margin=%5Bobject%20Object%5D&name=image.png&originHeight=278&originWidth=942&originalType=binary&ratio=1&rotation=0&showTitle=false&size=48215&status=done&style=none&taskId=ue75e9be1-49ee-410f-8189-4a270e41c47&title=&width=628)<br />c++通过提供特殊的对象指针,`this指针`,解决上述问题。`this指针`指向被调用的成员函数所属的`对象`。<br />c++规定,`this指针`是`隐含`在`对象成员函数内`的一种`指针`。当一个对象被创建后,它的每一个成员函数都含有一个系统自动生成的隐含`指针this`,用以保存这个对象的地址,也就是说虽然我们没有写上`this指针`,编译器在编译的时候也是会加上的。因此`this`也称为`指向本对象的指针`,this指针并不是对象的一部分,不会影响`sizeof(对象)`的结果。<br />`this指针`是C++实现封装的一种机制,它将对象和该对象调用的成员函数连接在一起,在外部看来,每一个对象都拥有自己的函数成员。一般情况下,并不写`this`,而是让系统进行默认设置。
  6. > this指针永远指向当前对象
  7. 成员函数通过`this指针`即可知道操作的是那个对象的数据。`this指针`是一种隐含指针,它隐含于每个类的非静态成员函数中。`this指针`无需定义,直接使用即可。
  8. > 静态成员函数内部没有this指针,静态成员函数不能操作非静态成员变量。
  9. **c++编译器对普通成员函数的内部处理**<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21972649/1654161864304-a18ca59a-7f80-4f90-9aa2-1f1fb31f6c88.png#clientId=u1d27943c-c36d-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=345&id=uffbebd6a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=518&originWidth=978&originalType=binary&ratio=1&rotation=0&showTitle=false&size=84135&status=done&style=none&taskId=u59b253ba-9913-4db3-bbf6-1e205faeea0&title=&width=652)
  10. <a name="EVG5g"></a>
  11. ### this指针的使用
  12. - 当形参和成员变量同名时,可用`this指针`来区分
  13. - 在类的非静态成员函数中返回对象本身,可使用`return *this`
  14. ```cpp
  15. class Person{
  16. public:
  17. //1. 当形参名和成员变量名一样时,this指针可用来区分
  18. Person(string name,int age){
  19. //name = name;
  20. //age = age; //输出错误
  21. this->name = name;
  22. this->age = age;
  23. }
  24. //2. 返回对象本身的引用
  25. //重载赋值操作符
  26. //其实也是两个参数,其中隐藏了一个this指针
  27. Person PersonPlusPerson(Person& person){
  28. string newname = this->name + person.name;
  29. int newage = this->age + person.age;
  30. Person newperson(newname, newage);
  31. return newperson;
  32. }
  33. void ShowPerson(){
  34. cout << "Name:" << name << " Age:" << age << endl;
  35. }
  36. public:
  37. string name;
  38. int age;
  39. };
  40. //3. 成员函数和全局函数(Perosn对象相加)
  41. Person PersonPlusPerson(Person& p1,Person& p2){
  42. string newname = p1.name + p2.name;
  43. int newage = p1.age + p2.age;
  44. Person newperson(newname,newage);
  45. return newperson;
  46. }
  47. int main(){
  48. Person person("John",100);
  49. person.ShowPerson();
  50. cout << "---------" << endl;
  51. Person person1("John",20);
  52. Person person2("001", 10);
  53. //1.全局函数实现两个对象相加
  54. Person person3 = PersonPlusPerson(person1, person2);
  55. person1.ShowPerson();
  56. person2.ShowPerson();
  57. person3.ShowPerson();
  58. //2. 成员函数实现两个对象相加
  59. Person person4 = person1.PersonPlusPerson(person2);
  60. person4.ShowPerson();
  61. system("pause");
  62. return EXIT_SUCCESS;
  63. }

类的const

const修饰成员函数

  • 用const修饰的成员函数时,const修饰this指针指向的内存区域,成员函数体内不可以修改本类中的任何普通成员变量。
  • 当成员变量类型符前用mutable修饰时例外。 ```cpp //const修饰成员函数 class Person{ public: Person(){
    1. this->mAge = 0;
    2. this->mID = 0;
    } //在函数括号后面加上const,修饰成员变量不可修改,除了mutable变量 void sonmeOperate() const{
    1. //this->mAge = 200; //mAge不可修改
    2. this->mID = 10;
    } void ShowPerson(){
    1. cout << "ID:" << mID << " mAge:" << mAge << endl;
    } private: int mAge; mutable int mID; };

int main(){

  1. Person person;
  2. person.sonmeOperate();
  3. person.ShowPerson();
  4. system("pause");
  5. return EXIT_SUCCESS;

}

  1. <a name="mWYe8"></a>
  2. ### const修饰对象(常对象)
  3. - 常对象只能调用`const的成员函数`
  4. - 常对象可访问 const 或非 const 数据成员,不能修改,除非成员用`mutable修饰`
  5. ```cpp
  6. class Person{
  7. public:
  8. Person(){
  9. this->mAge = 0;
  10. this->mID = 0;
  11. }
  12. void ChangePerson() const{
  13. mAge = 100;
  14. mID = 100;
  15. }
  16. void ShowPerson(){
  17. this->mAge = 1000;
  18. cout << "ID:" << this->mID << " Age:" << this->mAge << endl;
  19. }
  20. public:
  21. int mAge;
  22. mutable int mID;
  23. };
  24. void test(){
  25. const Person person;
  26. //1. 可访问数据成员
  27. cout << "Age:" << person.mAge << endl;
  28. //person.mAge = 300; //不可修改
  29. person.mID = 1001; //但是可以修改mutable修饰的成员变量
  30. //2. 只能访问const修饰的函数
  31. //person.ShowPerson();
  32. person.ChangePerson();
  33. }