100提几个问题

eg446yzk eg446yzk 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
家里的机器不能上2网,搜集了一个星期的问题到网吧来问各位拉!!!
1.在下列类中 输出结果为什么是这个样子的?
#include <iostream>
using namespace std;

class Monitor{
public:
static int count;
public:
void incident()
{
++count;
}
void decrement()
{
--count;
}
void print()
{
cout<<Monitor::count<<endl;
}
};
//-------------------
class Monitor2{
void *d;
public:
Monitor2(Monitor *p)
{
d=p;
Monitor *s;
s=reinterpret_cast<Monitor *>(d);
s->incident();
s->print();
cout<<"Monitor2"<<endl;
}
~Monitor2()
{
cout<<"~Monitor2"<<endl;
reinterpret_cast<Monitor *>(d)->decrement();
reinterpret_cast<Monitor *>(d)->print();
}
};
int Monitor::count=0;

void sdf()
{
Monitor m;
Monitor2 pp(&m);
}
Monitor m1;
Monitor2 pp1(&m1);

int main()
{
sdf();
}
这个函数定义了两次对象但是只调用了一次析构函数.
但是如果把那个全局定义的对象改成局部对象,那么就能正常析构这个对象.?????
在C++全局类对象是不是不调用析构函数的 ???
//----------------------------------------------------------------------------
...全文

相关推荐

评论 22
收藏 已收藏
分享
返回顶部
eg446yzk
C++ 语言 65,214 成员 250,514 内容