| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 621 人关注过本帖
标题:关于调用析构函数的问题,求教啊爱爱爱!
只看楼主 加入收藏
slfzzhm
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2010-7-16
结帖率:66.67%
收藏
已结贴  问题点数:10 回复次数:6 
关于调用析构函数的问题,求教啊爱爱爱!
#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
hahayezhe
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:湖南张家界
等 级:贵宾
威 望:24
帖 子:1386
专家分:6999
注 册:2010-3-8
收藏
得分:2 
什么时候消失,看它是已什么形式创建的,当前作用域!
2010-10-19 08:58
slfzzhm
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2010-7-16
收藏
得分:0 
回复 2楼 hahayezhe
我不太明白啊,我不知道对象作用域到哪里结束啊?能帮我注释下到那一句话对象结束作用的呢????
2010-10-19 22:14
上官伊萱
Rank: 1
等 级:新手上路
帖 子:5
专家分:2
注 册:2010-9-26
收藏
得分:2 
把我们书上的例子给你
#include<iostream>
using namespace std;
#include <string>
class Student
{
public:
    Student(int n,string nam,char s)
    {
        num=n;
        name=nam;
        sex=s;
        cout<<"Constructor called."<<endl;
    }
    ~Student()  //定义析构函数
    {
        cout<<"Destructor called."<<endl;
    }
    void display()
    {
        cout<<"num:"<<num<<endl;
        cout<<"name:"<<name<<endl;
        cout<<"sex:"<<sex<<endl;
    }
private:
    int num;
    string name;
    char sex;
};

int main()
{
    Student stud1(10010,"wang_li",'f');
    stud1.display();
    Student stud2(10012,"zhang_fun",'m');
    stud2.display();
    return 0;
}
结果是:
  Constructor called.  (执行stud1的构造函数)
  num:10010 (执行stud1的display函数)
  name:wang_li
  sex:f

  Constructor called. (执行stud2的构造函数)
  num:10011  (执行stud2的display函数)
  name:zhang_fun
  sex:m
  Destructor called.(执行stud2的析构函数)
  Destructor called.(执行stud1的析构函数)

先构造的后析构,后构造的先析构。
2010-10-20 15:35
personallife
Rank: 1
等 级:新手上路
帖 子:2
专家分:5
注 册:2007-9-17
收藏
得分:2 
两个对象是在程序结束的时候 可以尝试在程序结束的时候输出内容看看
如:
void exit()
{
    cout<<"the total weight of goods:"<<goods::gettotal()<<endl;
    //在此设断点观察
}
void main()
{
    atexit(exit);
    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;
}

[ 本帖最后由 personallife 于 2010-10-20 15:44 编辑 ]
2010-10-20 15:43
myth_feng
Rank: 2
等 级:论坛游民
威 望:1
帖 子:25
专家分:59
注 册:2010-8-11
收藏
得分:2 
同意2楼,看对象所在的作用域
goods g1(w);
goods g2(w);
都在main()中,只有当mian函数结束的时候才会调用析构函数。
2010-10-20 18:01
lyj2010lyj
Rank: 2
等 级:论坛游民
帖 子:25
专家分:27
注 册:2010-9-28
收藏
得分:2 
以下是引用personallife在2010-10-20 15:43:20的发言:

两个对象是在程序结束的时候 可以尝试在程序结束的时候输出内容看看
如:
void exit()
{
    cout<<"the total weight of goods:"<
结束符相对应
2010-10-20 19:15
快速回复:关于调用析构函数的问题,求教啊爱爱爱!
数据加载中...
 
   



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

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