作用:

    C++提供了初始化列表语法,用来初始化属性

    语法:构造函数():属性1(值1),属性2(值2)... {}

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. class Person{
    4. public:
    5. Person(){
    6. cout << "Person默认构造函数的调用" << endl;
    7. }
    8. Person(int num):m_Num(num){
    9. }
    10. ~Person(){
    11. cout << "Person析构函数的调用" << endl;
    12. }
    13. public:
    14. int m_Num;
    15. };
    16. Person func(Person per){
    17. return per;
    18. }
    19. int main(){
    20. Person person2(10); // 括号法调用有参构造函数
    21. cout << "m_Num = " << person2.m_Num << endl;
    22. return 0;
    23. }

    image.png