双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

求解呀大佬们,为什么test01在创建变量p时没有使用构造函数,也没有对临时对象析构,我认为test01和test02使用的析构函数应该一样才对吧。

Analyse_Complete Analyse_Complete 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
为什么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;
}
...全文
评论 4
收藏 已收藏
分享
返回顶部
Analyse_Complete
C++ 语言 65,214 成员 250,514 内容