• C++ 构造函数深入理解


    01_初始化参数列表.cpp

    #include<iostream>
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    struct Student{
        int id=1001;//成员变量或对象
        char *name=nullptr;
        int age=20;
        int score=99;
    
        Student(){cout<<"student default construction!"<<endl;}
        Student(int i,const char*n, int a,int s):id(i),age(a),score(s)
        {
        cout<<"student "<<n<<" construction!"<<endl;
            if(n){
                name =(char*)malloc(strlen(n)+1);
                strcpy(name,n);
            }
        }
        //拷贝构造函数定义
        //拷贝构造函数的有效参数,必须是该类的对象的引用
    #if 1
    //  Student(Student & s,int class_no=1){
        Student(Student & s){
        cout<<"student "<<s.name<<" copy construction!"<<endl;
            id=s.id;
    #if 1
            if(s.name){
                name =(char*)malloc(strlen(s.name)+1);
                strcpy(name,s.name);
            }
    #else
            name = s.name;
    #endif
            age=s.age;score=s.score;
        }
    #endif
        ~Student()
        {
            cout<<"student "<<name<<" destructor!"<<endl;
            if(name)free(name);
        }
    
    };//end class Student
    
    int get_stu_age(Student &s)
    {
        return s.age;
    }
    Student stu1(1001,"lisi",20,100);
    Student & get_stu()
    {
        return stu1;
    }
    //如果函数的参数和返回值是类类型的对象,那么在实参传递和函数返回时,会发生拷贝构造函数的调用;
    //如果想避免此类调用带来的巨大开销,可以使用引用
    
    int main()
    {
    //  Student stu2 = stu1;
        cout<<get_stu_age(stu1)<<endl;
        get_stu();
    
        return 0;
    }
    

    02_default.cpp

    #include<iostream>
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    struct Student{
        int id=1001;//成员变量或对象
        char *name=nullptr;
        int age=20;
        int score=99;
    
        //default 可以让编译器生成优化后的合成构造函数代码,适合发布产品使用
        Student()=default;
        Student(int i,const char*n, int a,int s){
        cout<<"student "<<n<<" construction!"<<endl;
            id=i;
            if(n){
                name =(char*)malloc(strlen(n)+1);
                strcpy(name,n);
            }
            age=a;score=s;
        }
        //拷贝构造函数定义
        //拷贝构造函数的有效参数,必须是该类的对象的引用
    #if 1
    //  Student(Student & s,int class_no=1){
        Student(Student & s){
        cout<<"student "<<s.name<<" copy construction!"<<endl;
            id=s.id;
    #if 1
            if(s.name){
                name =(char*)malloc(strlen(s.name)+1);
                strcpy(name,s.name);
            }
    #else
            name = s.name;
    #endif
            age=s.age;score=s.score;
        }
    #endif
        ~Student()
        {
            cout<<"student "<<name<<" destructor!"<<endl;
            if(name)free(name);
        }
    
    };//end class Student
    
    int get_stu_age(Student &s)
    {
        return s.age;
    }
    Student stu1(1001,"lisi",20,100);
    Student & get_stu()
    {
        return stu1;
    }
    //如果函数的参数和返回值是类类型的对象,那么在实参传递和函数返回时,会发生拷贝构造函数的调用;
    //如果想避免此类调用带来的巨大开销,可以使用引用
    
    int main()
    {
    //  Student stu2 = stu1;
        cout<<get_stu_age(stu1)<<endl;
        get_stu();
    
        return 0;
    }
    

    03_deleted.cpp

    #include<iostream>
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    struct Student{
        int id=1001;//成员变量或对象
        char *name=nullptr;
        int age=20;
        int score=99;
    
        Student()=default;
        Student(int i,const char*n, int a,int s){
        cout<<"student "<<n<<" construction!"<<endl;
            id=i;
            if(n){
                name =(char*)malloc(strlen(n)+1);
                strcpy(name,n);
            }
            age=a;score=s;
        }
        Student(Student & s){
        cout<<"student "<<s.name<<" copy construction!"<<endl;
            id=s.id;
            if(s.name){
                name =(char*)malloc(strlen(s.name)+1);
                strcpy(name,s.name);
            }
            age=s.age;score=s.score;
        }
        //delete是禁止某类参数传递的构造函数使用
        Student(const string s)=delete;
        /*
        {
            strcpy(name,s.c_str());
        }*/
        ~Student()
        {
            cout<<"student "<<name<<" destructor!"<<endl;
            if(name)free(name);
        }
    
    };//end class Student
    int main()
    {
        Student s("lisi");
        return 0;
    }
    

    04_explicit.cpp

    #include<iostream>
    #include <stdlib.h>
    #include <string.h>
    using namespace std;
    struct Student{
        int id=1001;//成员变量或对象
        char *name=nullptr;
        int age=20;
        int score=99;
    
        Student()=default;
        Student(int i,const char*n, int a,int s){
    //  cout<<"student "<<n<<" construction!"<<endl;
            id=i;
            if(n){
                name =(char*)malloc(strlen(n)+1);
                strcpy(name,n);
            }
            age=a;score=s;
        }
        //explicit要求构造函数必须直接初始化的方式调用,不允许隐式转换
    //  explicit 
            Student(const Student & s){
    //  cout<<"student "<<s.name<<" copy construction!"<<endl;
            id=s.id;
            if(s.name){
                name =(char*)malloc(strlen(s.name)+1);
                strcpy(name,s.name);
            }
            age=s.age;score=s.score;
        }
        //delete是禁止某类参数传递的构造函数使用
        Student(const char* n)
        {
            if(n){
                name=(char*)malloc(strlen(n)+1);
                strcpy(name,n);
            }
        }
        ~Student()
        {
    //      cout<<"student "<<name<<" destructor!"<<endl;
            if(name)free(name);
        }
    
    explicit void show_stu_name(const Student &s)
    {
        cout<<s.name<<endl;
    }
    
    };//end class Student
    
    
    
    int main()
    {
        Student s1(1001,"lisi",20,100);
        s1.show_stu_name(Student(Student("lisi")));
    //  Student s2 = s1;
        Student s2(s1);
    //  Student s3 = s1;
    
    
    
        return 0;
    }
    

    05_多参数初始化.cpp

    #include<iostream>
    
    using namespace std;
    
    struct A{
        int m[5];
    };
    
    struct B{
        int a,b,c,d,e;
    };
    
    struct C{
        int a,c,e;
        //该泛型接口只接受相同类型的初始化列表
        C(initializer_list<int> l){
            int i=0;
    //      initializer_list<int>::iterator it=l.begin();;
            for(auto k:l)
            {
                cout<<k<<" ";
                if(i ==0)a =k;
                if( i==2)c=k;
                if(i==4)e=k;
                i++;
            }
            cout<<endl;
        }
    };
    int main()
    {
        int k{10};
        cout<<k<<endl;
        A a ={1,2,3,4,5};
        cout<<a.m[0]<<endl;
        for(auto k:a.m)
            cout<<k<<" ";
        cout<<endl;
    
        B b ={1,2,3,4,5};
        cout<<"b.a="<<b.a<<endl;
        cout<<"b.b="<<b.b<<endl;
        cout<<"b.c="<<b.c<<endl;
        cout<<"b.d="<<b.d<<endl;
        cout<<"b.e="<<b.e<<endl;
    
        C c={5,6,7,8,9};
        cout<<c.a<<endl;
        cout<<c.c<<endl;
        cout<<c.e<<endl;
    
        return 0;
    }
  • 相关阅读:
    网络学习笔记
    zabbix4.2学习笔记系列
    ansible2.7学习笔记系列
    记一次磁盘UUID不能识别故障处理
    白话ansible-runner--1.环境搭建
    kubernetes的思考
    计算机网络原理精讲第六章--应用层
    计算机网络原理精讲第五章--传输层
    centos7下LVM挂载和扩容
    多线程下载命令--axel
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384256.html
Copyright © 2020-2023  润新知