默认情况下,c++编译器至少给一个类添加3个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
构造函数调用规则如下:
- 如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
- 如果用户定义拷贝构造函数,c++不会再提供其他构造函数
#include<iostream>
using namespace std;
//构造函数的调用规则
//1、创建一个类,C++编译器会给每个类都添加至少3个函数
//默认构造(空实现)
//析构函数(空实现)
//拷贝构造(值拷贝)
//2、
//如果我们写了有参构造函数,编译器就不再提供默认构造,依然会提供拷贝构造
//如果我们写了拷贝构造函数,编译器就不再提供其他普通构造函数
class Person {
public:
/*Person() {
cout << "Person的默认构造函数调用" << endl;
}*/
/*Person(int age) {
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}*/
Person(const Person& p) {
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person() {
cout << "Person的析构函数调用" << endl;
}
int m_Age;
};
//void test01() {
// Person p1;
// p1.m_Age = 18;
//
// Person p2(p1);
//
// cout << "p2的年龄为:" << p2.m_Age << endl;
//}
void test02() {
//Person p3(28);
//Person p4(p3);
//cout << "p4的年龄为:" << p4.m_Age << endl;
}
int main() {
//test01();
//test02();
system("pause");
return 0;
}
默认情况下,c++编译器至少给一个类添加3个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
class Person {
public:
Person() {
cout << "Person的默认构造函数调用" << endl;
}
Person(int age) {
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
Person(const Person& p) {
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person() {
cout << "Person的析构函数调用" << endl;
}
int m_Age;
};
void test01() {
Person p1;
p1.m_Age = 18;
Person p2(p1);
cout << "p2的年龄为:" << p2.m_Age << endl;
}
int main(){
test01();
return 0;
}
运行结果
把拷贝构造函数注释掉
class Person {
public:
Person() {
cout << "Person的默认构造函数调用" << endl;
}
Person(int age) {
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
/*Person(const Person& p) {
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}*/
~Person() {
cout << "Person的析构函数调用" << endl;
}
int m_Age;
};
void test01() {
Person p1;
p1.m_Age = 18;
Person p2(p1);
cout << "p2的年龄为:" << p2.m_Age << endl;
}
int main(){
test01();
return 0;
}
运行结果
p2依然显示年龄,并未有Person的拷贝构造函数,并未报错,因此可知编译器提供了默认拷贝构造函数,即m_Age = age;
构造函数调用规则如下:
- 如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
- 如果用户定义拷贝构造函数,c++不会再提供其他构造函数
1.如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
//提供有参构造函数,但不提供默认构造函数
class Person {
public:
/*Person() {
cout << "Person的默认构造函数调用" << endl;
}*/
Person(int age) {
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
/*Person(const Person& p) {
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}*/
~Person() {
cout << "Person的析构函数调用" << endl;
}
int m_Age;
};
void test02(){
Person p3;
}
int main(){
test02();
return 0;
}
此时会报错
因为我们提供了有参构造函数,所有编译器不再提供默认构造函数,所以会出现报错
//提供有参构造函数,但不提供拷贝构造函数
class Person {
public:
/*Person() {
cout << "Person的默认构造函数调用" << endl;
}*/
Person(int age) {
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
/*Person(const Person& p) {
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}*/
~Person() {
cout << "Person的析构函数调用" << endl;
}
int m_Age;
};
void test02(){
Person p3(28);
Person p4(p3);
cout<< "p4的年龄为:" << p4.m_Age << endl;
}
int main(){
test02();
return 0;
}
运行结果
自己提供有参构造函数,即使我们不提供拷贝构造函数,编译器也会提供默认拷贝构造函数
- 如果用户定义拷贝构造函数,c++不会再提供其他构造函数
//提供拷贝构造函数,但不提供普通构造函数
class Person {
public:
/*Person() {
cout << "Person的默认构造函数调用" << endl;
}*/
/*Person(int age) {
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}*/
Person(const Person& p) {
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person() {
cout << "Person的析构函数调用" << endl;
}
int m_Age;
};
void test02(){
Person p3;
}
int main(){
test02();
return 0;
}
此时会报错
因为我们提供了拷贝构造函数,但并没有提供默认构造函数,编译器也不会提供默认构造函数,因此会报错。
总结:写了有参构造,编译器不会提供默认构造,会提供拷贝构造;写了拷贝构造,不会提供默认构造和有参构造。