| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 621 人关注过本帖
标题:关于调用析构函数的问题,求教啊爱爱爱!
取消只看楼主 加入收藏
slfzzhm
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2010-7-16
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:1 
关于调用析构函数的问题,求教啊爱爱爱!
#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






}
搜索更多相关主题的帖子: 函数 
2010-10-18 22:19
slfzzhm
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2010-7-16
收藏
得分:0 
回复 2楼 hahayezhe
我不太明白啊,我不知道对象作用域到哪里结束啊?能帮我注释下到那一句话对象结束作用的呢????
2010-10-19 22:14
快速回复:关于调用析构函数的问题,求教啊爱爱爱!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011431 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved