关于调用析构函数的问题,求教啊爱爱爱!
#include<iostream>using namespace std;
class goods{
private:
static int totalweight;
int weight;
public:
goods(int w)
{
weight=w;
totalweight+=w;
}
goods(goods&gd)
{
weight=gd.weight;
totalweight+=weight;
}
~goods()
{
totalweight-=weight;
}
int getwg()
{
return weight;
}
static int gettotal()
{
return totalweight;
}
};
int goods::totalweight=0;
void main()
{
int w ;
cout<<"the initial weight of goods:"<<goods::gettotal()<<endl;
cin>>w;
goods g1(w);//输入25
cin>>w;
goods g2(w);//输入60
cout<<"the total weight of goods:"<<goods::gettotal()<<endl;
输出为the initial weight of goods:0
the total weight of goods:85
用类建立对象的时候不是会自动调用构造函数吗??但是对象消失的时候会自动调用析构函数,~goods()
{
totalweight-=weight;
}
我的问题就是这道题目的对象在什么时候消失的???能不能帮我注释一下啊!
我的意思是输出应该是the initial weight of goods:0
the total weight of goods:0
}