构造函数以及析构函数的调用
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "Person的无参构造函数。" << endl;
}
Person(int a)
{
age = a;
cout << "Person的有参构造函数。" << endl;
}
Person(const Person& p)
{
age = p.age;
cout << "Person的拷贝构造函数。" << endl;
}
~Person()
{
cout << "Person的析构函数。" << endl;
}
public:
int age;
};
void test01()
{
//1、括号法
Person P1;//默认构造函数调用
Person P2(10);//有参构造函数调用
Person P3(P1);//拷贝构造函数调用
//2、显示法
Person P4;
Person P5 = Person(10);//有参构造函数调用
Person P6 = Person(P5);//拷贝函数调用
Person(10);//匿名对象,不能利用拷贝函数初始化匿名对象
//3、隐式转换法
Person P7 = 10;//相当于 Person P4=Person(10) 有参构造函数调用
Person P8 = P7;//拷贝函数调用
}
int main()
{
test01();
system("pause");
return 0;
}
注意函数调用的三种方法