默认情况下,c++编译器至少给一个类添加3个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行值拷贝
造函数调用规则如下:
调用规则:
如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
如果用户定义拷贝构造函数,c++不会再提供其他构造函数
示例:
#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
int m_age;
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
~Person()
{
cout << "Person的默认析构函数" << endl;
}
Person(int age)
{
m_age = age;
cout << "Person的有参构造函数调用" << endl;
}
Person(const Person& p)
{
cout << "Person的拷贝构造函数调用" << endl;
m_age = p.m_age;
}
};
void test01()
{
Person p;
p.m_age = 81;
Person p1(p);
cout << p1.m_age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
很完美的一个例程代码,其输出结果为:
但是我们如果把拷贝构造函数注释掉之后,这时p的年龄又会是多少呢?
#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
int m_age;
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
~Person()
{
cout << "Person的默认析构函数" << endl;
}
Person(int age)
{
m_age = age;
cout << "Person的有参构造函数调用" << endl;
}
/*Person(const Person& p)
{
cout << "Person的拷贝构造函数调用" << endl;
m_age = p.m_age;
}*/
};
void test01()
{
Person p;
p.m_age = 81;
Person p1(p);
cout << p1.m_age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
我们 会发现p的年龄还是81没有发生变化的。但是输出也有部分变化,比如后面的那个就没有输出”拷贝构造函数的调用“,为什么?
因为我们给拷贝构造函数注释掉了,在默认拷贝构造函数里面只有值的拷贝,没有输出的那句话,所以值是系统给默认拷贝过来了的二文字没有输出。
1.如果用户定义有参构造函数,c++不在提供默认无参构造,但是会提供默认拷贝构造
假设代码如下:
#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
int m_age;//默认构造函数被我们注释掉了
/*Person()
{
cout << "Person的默认构造函数调用" << endl;
}*/
~Person()
{
cout << "Person的默认析构函数" << endl;
}
Person(int age)
{
m_age = age;
cout << "Person的有参构造函数调用" << endl;
}
//Person(const Person& p)
//{
// cout << "Person的拷贝构造函数调用" << endl;
// m_age = p.m_age;
//}
};
void test01()
{
Person p;
p.m_age = 81;
Person p1(p);
cout << p1.m_age << endl;
}
void test02()
{
Person p;
}
int main()
{
// test01();
test02();
system("pause");
return 0;
}
编译会出现错误(因为我们如果设置了有参构造函数之后,系统就不会创建默认构造函数)
#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
int m_age;
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
~Person()
{
cout << "Person的默认析构函数" << endl;
}
Person(int age)
{
m_age = age;
cout << "Person的有参构造函数调用" << endl;
}
//Person(const Person& p)
//{
// cout << "Person的拷贝构造函数调用" << endl;
// m_age = p.m_age;
//}
};
void test01()
{
Person p;
p.m_age = 81;
Person p1(p);
cout << p1.m_age << endl;
}
void test02()
{
Person p(28);
Person p1(p);
cout << p1.m_age << endl;
}
int main()
{
// test01();
test02();
system("pause");
return 0;
}
其输出结果为:
验证了就算我们创建了有参构造函数,系统也会提供默认拷贝构函数。
2.如果用户定义拷贝构造函数,c++不会再提供其他构造函数
同样的代码,我们将系统默认构造函数和有参构造函数给注释掉
#include<iostream>
using namespace std;
//创建一个类C++自动添加3个函数
class Person
{
public:
int m_age;
/*Person()
{
cout << "Person的默认构造函数调用" << endl;
}
~Person()
{
cout << "Person的默认析构函数" << endl;
}
Person(int age)
{
m_age = age;
cout << "Person的有参构造函数调用" << endl;
}*/
Person(const Person& p)
{
cout << "Person的拷贝构造函数调用" << endl;
m_age = p.m_age;
}
};
void test01()
{
Person p;
p.m_age = 81;
Person p1(p);
cout << p1.m_age << endl;
}
void test02()
{
Person p(28);
Person p1(p);
cout << p1.m_age << endl;
}
int main()
{
// test01();
test02();
system("pause");
return 0;
}
系统不会提供注释掉的两个函数。
总结,如果我们定义了有参构造函数系统会提供默认拷贝构造函数而不会提供默认构造函数,但如果我们定义了拷贝构造函数,系统就不会创建默认构造函数和有参构造函数。