构造函数调用规则

当只设置了有参构造函数时,编译器会默认不再有无参构造函数和析构函数,但是有拷贝构造函数

#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
    int age;
public:
    Student(int a){
        age = a;
    }
}
int main(){
    system("chcp 65001");
    Student stu1;
    system("pause");
}

 

 

报错,但是当进行拷贝构造函数时,

#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
    int age;
public:
    Student(int a){
        age = a;
    }
    void printAge(){
        cout<<age<<endl;
    }
};
int main(){
    system("chcp 65001");
    Student stu1(12);
    Student stu2(stu1);
    stu2.printAge();
    system("pause");
    return 0;
}

 

当只设置了拷贝函数时,默认的无参构造函数和有参构造函数和析构函数都不存在了

posted @ 2020-09-24 21:51  磊神加特林  阅读(165)  评论(0)    收藏  举报