C++ 构造函数深入理解

01_初始化参数列表.cpp

#include
#include 
#include 
using namespace std;
struct Student{
    int id=1001;//成员变量或对象
    char *name=nullptr;
    int age=20;
    int score=99;

    Student(){cout<<"student default construction!"<int i,const char*n, int a,int s):id(i),age(a),score(s)
    {
    cout<<"student "<" construction!"<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 "<" copy construction!"<#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 "<" destructor!"<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<return 0;
}

02_default.cpp

#include
#include 
#include 
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 "<" construction!"<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 "<" copy construction!"<#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 "<" destructor!"<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<return 0;
}

03_deleted.cpp

#include
#include 
#include 
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 "<" construction!"<if(n){
            name =(char*)malloc(strlen(n)+1);
            strcpy(name,n);
        }
        age=a;score=s;
    }
    Student(Student & s){
    cout<<"student "<" copy construction!"<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 "<" destructor!"<if(name)free(name);
    }

};//end class Student
int main()
{
    Student s("lisi");
    return 0;
}

04_explicit.cpp

#include
#include 
#include 
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 "<
        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 "<
        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 "<
        if(name)free(name);
    }

explicit void show_stu_name(const Student &s)
{
    cout<//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

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::iterator it=l.begin();;
        for(auto k:l)
        {
            cout<" ";
            if(i ==0)a =k;
            if( i==2)c=k;
            if(i==4)e=k;
            i++;
        }
        cout<int main()
{
    int k{10};
    cout<1,2,3,4,5};
    cout<0]<for(auto k:a.m)
        cout<" ";
    cout<1,2,3,4,5};
    cout<<"b.a="<cout<<"b.b="<cout<<"b.c="<cout<<"b.d="<cout<<"b.e="<5,6,7,8,9};
    cout<cout<cout<return 0;
}

你可能感兴趣的:(c++,C++-构造函数深入)