• C++类与结构体、this指针(二)


    结构体嵌套结构体(活用双“.”)

    作用:在结构体中的成员可以是另一个结构体,例如每个老师有自己的信息,包括他带的学生,然后每个学生也有自己的信息。

    1. #include<iostream>
    2. Using namespace std;
    3. //定义学生的结构体
    4. struct student
    5. {
    6. int name;
    7. int age;
    8. int score;
    9. }
    10. //定义老师的结构体
    11. struct teacher
    12. {
    13. int id;
    14. string name;
    15. int age;
    16. struct student;
    17. }
    18. int main()
    19. {
    20. teacher t; //声明一个老师
    21. t.name=”wang” //初始化老师的数据
    22. t.age=50;
    23. t.stu.name=”Li”; //初始化老师里面的学生
    24. t.stu.age=20;
    25. t.stu.score=60;
    26. }

    结构体做函数参数(是否改变实参参数)

        作用:将结构体作为参数向函数中传递,传递方式有两种:值传递(形参改变不改变实参)和地址传递(形参改变将改变实参)。

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. //定义学生的结构体
    5. struct student
    6. {
    7. string name;
    8. int age;
    9. int score;
    10. };
    11. //值传递
    12. void printStudent1(student s)
    13. {
    14. s.age = 100; //在值传递里面修改了年龄
    15. cout << s.name << s.age << s.score << endl;
    16. }
    17. //地址传递
    18. void printStudent2(student* s)
    19. {
    20. //虽然上面修改了形参,但是输出的实参没有变化,因此程序运行到printStudent2
    21. //时,age依然等于20
    22. cout << s->name << s->age << s->score << endl;
    23. //修改地址,就会发生改变
    24. s->age = 100;
    25. cout << s->name << s->age << s->score << endl;
    26. }
    27. int main()
    28. {
    29. student s;
    30. s.name = "Wang";
    31. s.age = 20;
    32. s.score = 80;
    33. printStudent1(s);
    34. printStudent2(&s);
    35. return 0;
    36. }

    如果你不想修改主函数数据的话,那就用值传递。反之若修改主函数中的数据,则使用地址传递。

    结构体中const的使用

        作用:用const来防止误操作。

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. //定义学生的结构体
    5. struct student
    6. {
    7. string name;
    8. int age;
    9. int score;
    10. };
    11. //地址传递:将函数中的形参改为指针,可以减少内存空间
    12. void printStudent(const student *s)
    13. {
    14. //若在函数体内不小心改写了消息,此时会报错,可以防止误操作
    15. s->age = 150;
    16. cout << s->name << s->age << s->score << endl;
    17. }
    18. int main()
    19. {
    20. student s;
    21. s.name = "Wang";
    22. s.age = 20;
    23. s.score = 80;
    24. printStudent(&s);
    25. return 0;
    26. }

    struct和class的区别

        在C++中,这两个唯一的区别就是默认的访问权限不同,struct默认为public,class默认为private。

    1. #include<iostream>
    2. using namespace std;
    3. class C1
    4. {
    5. int m_A;//默认权限为私有
    6. };
    7. struct C2
    8. {
    9. int m_A;//默认为私有
    10. };
    11. int main()
    12. {
    13. C1 c1;
    14. c1.m_A = 100; //错误,类外不能访问私有成员
    15. C2 c2;
    16. c2.m_A = 100; //正确,因为结构体中默认权限为public
    17. return 0;
    18. }

    构造函数与析构函数

    执行顺序

    1. #include<iostream>
    2. using namespace std;
    3. class Person
    4. {
    5. public:
    6. Person()
    7. {
    8. cout << "构造函数被调用了!" << endl;
    9. }
    10. //构造函数没有返回值,不用写void
    11. //函数名与类名相同,且可以有参数,可以重载
    12. //在创建对象时,构造函数自动被调用且仅被调用一次
    13. //析构函数
    14. //没有返回值,在名称前+“~”
    15. //析构函数不能有参数,因此不能重载
    16. //对象在销毁前会自动调用析构函数,切仅一次
    17. ~Person() {
    18. cout << "析构函数被调用了" << endl;
    19. }
    20. };
    21. void test01()
    22. {
    23. Person p;
    24. }
    25. int main()
    26. {
    27. test01();
    28. return 0;
    29. }
    1. #include<iostream>
    2. using namespace std;
    3. class Person
    4. {
    5. public:
    6. Person()
    7. {
    8. cout << "构造函数被调用了!" << endl;
    9. }
    10. ~Person() {
    11. cout << "析构函数被调用了" << endl;
    12. }
    13. };
    14. int main()
    15. {
    16. Person p;
    17. //可以发现,只有构造函数的语句被输出了,这是因为左侧的test01函数执行结束后将自动调用析构函数,打印语句。但是该例中Person p语句仅执行了构造函数,由于main函数结束后才能打印语句,因此在dos界面的我们是不一定可以看到的。
    18. //关闭dos界面的瞬间,将释放对象,调用析构,打印语句。
    19. return 0;
    20. }

    构造函数的分类及调用

    按有无参数,构造函数可分为有参构造和无参构造(默认构造);按类型可分为普通构造和拷贝构造。

    构造函数有三种调用方式:括号法,显示法和隐式转换法。

    拷贝构造需要传入一个相同的数据类型的对象。

    不要用括号法调用默认构造,会被编译器误解为函数的声明。

    1. #include<iostream>
    2. using namespace std;
    3. class Person
    4. {
    5. public:
    6. //普通构造
    7. //无参构造
    8. Person() {
    9. cout << "调用无参构造函数" << endl;
    10. }
    11. //有参构造(默认构造函数)
    12. Person(int a) {
    13. age = a;
    14. cout << "调用有参构造函数" << endl;
    15. }
    16. //拷贝构造
    17. Person(const Person& p) {
    18. age = p.age; //将传输的人的身上的所有属性拷贝到当前对象身上
    19. cout << "调用拷贝构造函数" << endl;
    20. }
    21. //析构函数
    22. ~Person()
    23. {
    24. cout << "调用析构函数" << endl;
    25. }
    26. private:
    27. int age;
    28. };
    29. void test01()
    30. {
    31. //括号法调用
    32. Person p1(10); //调用有参构造函数
    33. Person p2;
    34. Person p2(); //并不会创建对象,编译器会认为这是一个函数的声明
    35. Person p3(p2);//调用拷贝构造函数
    36. //显示法调用
    37. Person p1;
    38. Person p2 = Person(10);//显示法调用有参构造
    39. Person p2 = Person(p1);//显示法调用拷贝构造
    40. Person(10);//这是一个匿名对象,当前行执行结束后,系统会立即回收
    41. Person(p3);//不要利用拷贝构造函数初始化匿名对象,编译器会认为这是一个对象的声明
    42. //隐式转换法调用
    43. Person p4 = 10;//相当于Person p4= person(10);
    44. Person p5 = p4;//相当于拷贝构造
    45. }

    那么,什么时候使用拷贝构造函数?

    通常有三种情况:一是使用一个已经创建完毕的对象来初始化另一个对象;二是值传递的方式给函数参数传值;三是以值方式返回局部对象。

    1. #include<iostream>
    2. using namespace std;
    3. class Person
    4. {
    5. public:
    6. Person()
    7. {
    8. cout << "Person的默认构造函数调用" << endl;
    9. }
    10. Person(int age)
    11. {
    12. m_Age = age;
    13. cout << "Person的有参构造函数调用" << endl;
    14. }
    15. Person(const Person& p)
    16. {
    17. m_Age = p.m_Age;
    18. cout << "Person的拷贝构造函数调用" << endl;
    19. }
    20. ~Person()
    21. {
    22. cout << "Person的析构函数调用" << endl;
    23. }
    24. int m_Age;
    25. };
    26. //1、使用一个已经创建完毕的对象来初始化一个新对象
    27. void test01()
    28. {
    29. Person p1(20);
    30. Person p2(p1);
    31. }
    32. //2、以值传递的方式给函数参数传值
    33. void doWork(Person p) //由实参传往形参的时候,会有一个临时的拷贝
    34. {
    35. }
    36. void test02()
    37. {
    38. Person p;
    39. doWork(p);
    40. }
    41. //3、值的方式返回,返回的是一个拷贝
    42. Person doWork2()
    43. {
    44. Person p1; //并不会直接返回这个对象,返回的是这个对象的拷贝
    45. cout << (int*)&p1 << endl;
    46. return p1;
    47. }
    48. void test03()
    49. {
    50. Person p = doWork2();
    51. cout << (int*)&p << endl; //p的地址和p1的地址不一样
    52. }
    53. int main()
    54. {
    55. //test01();
    56. //test02();
    57. test03();
    58. return 0;
    59. }

    默认情况下,C++会给一个类添加至少3个函数:默认构造函数(函数体为空),析构函数(函数体为空)和默认拷贝构造函数(对属性进行值拷贝)。如果用户谢了有参构造函数,则C++不再提供默认无参构造函数,但会提供默认拷贝构造函数。如果用户定义拷贝构造函数,C++不会再提供其他构造函数。

    深拷贝与浅拷贝

    浅拷贝是简单的赋值拷贝操作,通过“=”号就可实现。深拷贝是指在堆区重新申请空间,进行拷贝操作。

    例如当我们使用浅拷贝复制对象,如果被拷贝的对象中存在堆区数据,则在执行析构函数时会释放两次堆区的数据,带来异常,如下:

    1. #include<iostream>
    2. using namespace std;
    3. class Person
    4. {
    5. public:
    6. Person()
    7. {
    8. cout << "Person的默认构造函数调用" << endl;
    9. }
    10. Person(int age, int height)
    11. {
    12. m_Age = age;
    13. new int(height); //堆区开辟内存
    14. cout << "Person的有参构造函数调用" << endl;
    15. }
    16. ~Person()
    17. {
    18. if (m_Height != NULL) //释放堆区数据
    19. delete m_Height;
    20. m_Height = NULL; //防止出现野指针
    21. cout << "Person的析构构造函数调用" << endl;
    22. }
    23. int m_Age;
    24. int* m_Height; //将指针创建在堆区
    25. };
    26. void test01()
    27. {
    28. Person p1(18,160);
    29. cout << "p1的年龄为:" << p1.m_Age<<"身高为" <<p1.m_Height<< endl;
    30. Person p2(p1); //浅拷贝操作,把指针也拷贝过去了
    31. cout << "p2的年龄为:" << p2.m_Age<<"身高为" <<p2.m_Height<< endl;
    32. //p2执行析构函数时会释放一次堆区数据,p1也会执行一次。
    33. //因此使用浅拷贝,将会重复释放堆区内存。
    34. }
    35. int main()
    36. {
    37. test01();
    38. return 0;
    39. }

    使用深拷贝可以解决上述问题。即从新找一块堆区空间存放new的数据。

    1. #include<iostream>
    2. using namespace std;
    3. class Person
    4. {
    5. public:
    6. Person()
    7. {
    8. cout << "Person的默认构造函数调用" << endl;
    9. }
    10. Person(int age, int height)
    11. {
    12. m_Age = age;
    13. new int(height); //堆区开辟内存
    14. cout << "Person的有参构造函数调用" << endl;
    15. }
    16. //自己定义拷贝构造函数,解决浅拷贝的问题
    17. Person(const Person& p)
    18. {
    19. cout << "Person拷贝构造函数的调用" << endl;
    20. m_Age = p.m_Age;
    21. //m_Height = p.m_Height; //编译器默认的浅拷贝操作
    22. m_Height = new int(*p.m_Height); //深拷贝
    23. }
    24. ~Person()
    25. {
    26. if (m_Height != NULL) //释放堆区数据
    27. delete m_Height;
    28. m_Height = NULL; //防止出现野指针
    29. cout << "Person的析构构造函数调用" << endl;
    30. }
    31. int m_Age;
    32. int* m_Height; //将指针创建在堆区
    33. };
    34. void test01()
    35. {
    36. Person p1(18,160);
    37. cout << "p1的年龄为:" << p1.m_Age<<"身高为" <<p1.m_Height<< endl;
    38. Person p2(p1); //浅拷贝操作,把指针也拷贝过去了
    39. cout << "p2的年龄为:" << p2.m_Age<<"身高为" <<p2.m_Height<< endl;
    40. //p2执行析构函数时会释放一次堆区数据,p1也会执行一次。
    41. //因此使用浅拷贝,将会重复释放堆区内存。
    42. }
    43. int main()
    44. {
    45. test01();
    46. return 0;
    47. }

    类对象作为类成员(构造与析构的顺序)

        C++类中的成员可以是另一个类中的对象,我们称该成员为对象成员。例如

    clase A{}

    clase B

    {

       A a;

    }

    B类中有对象A作为成员,因此A为对象成员,那么创建B对象时,A的构造会先于B。即当其他类的对象作为本类的数据成员时,构造的时候先构造其他类的对象,然后再构造自身。

    对B进行析构时,B先析构,然后A再析构。即析构的顺序与构造相反。

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Phone
    5. {
    6. public:
    7. string m_Pname;
    8. Phone(string pName)
    9. {
    10. m_Pname = pName;
    11. cout << "Phone的构造" << endl;
    12. }
    13. };
    14. class Person
    15. {
    16. public:
    17. Person(string name, string pName) :m_name(name), m_Phone(pName) {
    18. cout << "Person的构造" << endl;
    19. }
    20. //相当于Phone m_Phone=pName,隐式转换法
    21. string m_name;
    22. Phone m_Phone;
    23. };
    24. void test01()
    25. {
    26. Person p("Zhang", "iphone");
    27. cout << p.m_name << " has " << p.m_Phone.m_Pname << endl;
    28. }
    29. int main()
    30. {
    31. test01();
    32. return 0;
    33. }

    对象模型与this指针

    对象的存储空间

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

    成员变量和成员函数是分开存储的。

    case 1:

    空对象占用的内存空间为1个字节

    原因:C++编译器会给每个空对象也分配1个字节的空间,是为了区分空对象占内存的位置。例如说连续两个空对象,它们需要占用不同的内存空间,因此需要一个字节来区分。

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. };
    7. void test01()
    8. {
    9. Person p;
    10. cout << "size of p = " << sizeof(p) << endl;
    11. }
    12. int main()
    13. {
    14. test01();
    15. return 0;
    16. }

    case 2:

    对象将占用4个字节,当不是空的时,就按照int来分配4个字节的内存。

    也就是说,非静态成员属于类的对象的内存。

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. int m_A;//非静态成员变量
    7. };
    8. void test02()
    9. {
    10. Person p;
    11. cout << "size of p = " << sizeof(p) << endl;
    12. }
    13. int main()
    14. {
    15. test02();
    16. return 0;
    17. }

    case 3:

    对象仍然占用4个字节,说明静态成员变量不在类的对象上。

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. int m_A;//非静态成员变量
    7. static int m_B;//静态成员变量
    8. };
    9. int Person::m_B = 10; //静态数据成员只能在类外进行初始化
    10. void test02()
    11. {
    12. Person p;
    13. cout << "size of p = " << sizeof(p) << endl;
    14. }
    15. int main()
    16. {
    17. test02();
    18. return 0;
    19. }

    case 4:

    仍然占用4个字节,说明非静态成员函数也不再类的对象上。

    静态成员函数更不会在对象上。

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. int m_A;//非静态成员变量
    7. static int m_B;//静态成员变量
    8. void func();//非静态成员函数
    9. };
    10. int Person::m_B = 10;
    11. void test02()
    12. {
    13. Person p;
    14. cout << "size of p = " << sizeof(p) << endl;
    15. }
    16. int main()
    17. {
    18. test02();
    19. return 0;
    20. }

    this指针

    C++成员变量和成员函数是分开存储的。每一个非静态成员函数只会产生一份函数实例,也就是说多个同类型的对象会公用一块代码。

    C++通过提供特殊的对象指针,即this指针来区分成员函数被哪些对象调用。this指针指向被调用的成员函数所属的对象。即如果对象p1调用func(),则this指针指向p1;如果对象p2调用func(),则this指针指向p2。

    this指针是隐含每一个非静态成员函数内,不需要定义。

    This指针的作用:1、当函数的形参和成员变量同名时,可以用this来区分;2、在类的非静态成员函数中返回对象本身,可用return *this。

    this指针作用1:解决名称冲突

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. public:
    7. Person(int age)
    8. {
    9. //age = age;
    10. this->age=age;//this指针指向的是被调用的成员函数所属的对象,此时构造函数的this指向p1。
    11. }
    12. int age; //成员名称和传入的形参名称应该不同
    13. };
    14. void test01()
    15. {
    16. Person p1(18);
    17. cout << "age of p1 is" << p1.age << endl;
    18. }
    19. int main()
    20. {
    21. test01();
    22. return 0;
    23. }

    this指针作用2:

    返回对象本身用*this(链式编程思想)

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. public:
    7. Person(int age)
    8. {
    9. this->age = age;
    10. }
    11. Person PersonAddAge(Person& p)
    12. {
    13. this->age += p.age; //把对象p的年龄加到自己上面来
    14. //this是一个指向p2的指针,*this就是p2这个对象的本体,因此函数返回值应该是一个引用
    15. return *this;//指向本体
    16. }
    17. int age;
    18. };
    19. void test02()
    20. {
    21. Person p1(10);
    22. Person p2(10);
    23. //p2.PersonAddAge(p1);
    24. //如果我想多加几次呢?
    25. p2.PersonAddAge(p1).PersonAddAge(p1);
    26. cout << "age of p2 is " << p2.age << endl;
    27. }
    28. int main()
    29. {
    30. test02();
    31. return 0;
    32. }

    使用空指针访问成员函数

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

    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. class Person
    5. {
    6. public:
    7. void showClassName()
    8. {
    9. cout << "this is Person class" << endl;
    10. }
    11. void showPersonAge()
    12. {
    13. cout << "age = " << m_Age << endl;
    14. //该句等同于:cout << "age = " <<this-> m_Age << endl;
    15. }
    16. int m_Age;
    17. };
    18. void test01()
    19. {
    20. Person* p = NULL;
    21. p->showClassName();
    22. p->showPersonAge();//由于this没有指向确切的对象,因此用this指针访问数据成员出错
    23. //即如果我们用this指针时,不能用空指针
    24. }
    25. int main()
    26. {
    27. test01();
    28. return 0;
    29. }
  • 相关阅读:
    Web阶段一 静态网页
    在10.24这个特殊的日子里,带你详细解读1024!
    GPT实战系列-LangChain如何构建基通义千问的多工具链
    苏宁API:一键搜索,海量商品任你选!
    dubbo面试题
    开源在线客服系统源码微信小程序
    VMware 虚拟机安装 OpenWrt 作旁路由 单臂路由 img 镜像转 vmdk 旁路由无法上网 没网络
    构建 Audio Unit 应用程序
    Vue自定义指令
    正则表达式符号
  • 原文地址:https://blog.csdn.net/Lao_tan/article/details/125422909