为何先构造的对象后析构,后构造的函数却先析构呢?

bjwqt bjwqt 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
为何先构造的对象后析构,后构造的函数却先析构呢?

#include <iostream>
using namespace std;

class Student
{
public:
Student(int num=1,char*name="zgh");
~Student();
out();
private:
int num;
char *name;
};

Student::Student(int num,char* name)
{
this->num=num;
this->name=name;
cout << "对象构造!" << endl;
}

Student::~Student()
{
cout << "对象析构!此时 num = " << num << endl;
}

Student::out()
{
cout << num << " " << name << endl;
}


int main()
{
Student t1(1,"aaa");
t1.out();
cout << "--------------------" << endl;
Student t2(2,"aaa");
t2.out();
return 0;
}

...全文
评论 4
收藏 已收藏
分享
返回顶部
bjwqt
VC/MFC 16,550 成员 421,606 内容