求解呀大佬们,为什么test01在创建变量p时没有使用构造函数,也没有对临时对象析构,我认为test01和test02使用的析构函数应该一样才对吧。
为什么test01在创建变量p时没有使用构造函数,也没有对临时对象析构,我认为test01和test02使用的析构函数应该一样才对吧。
#include<iostream>
using namespace std;
class Person
{
int m_age;
public:
Person()
{
cout << "默认构造" << endl;
}
Person(int age):m_age(age)
{
cout << "有参构造" << endl;
}
Person(const Person &p)
{
m_age = p.m_age;
cout << "复制构造函数" << endl;
}
~Person()
{
cout << "析构构造" << endl;
}
};
Person dowork()
{
Person p1;
return p1;
}
void test01()
{
Person p = dowork();
}
void test02()
{
Person p;
p = dowork();
}
int main()
{
cout << "test01 :" << endl;
test01();
cout << "test02 :" << endl;
test02();
system("pause");
return 0;
}