| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1590 人关注过本帖
标题:如何设置调用析构函数才能实现以下效果
只看楼主 加入收藏
luoj75
Rank: 2
等 级:论坛游民
帖 子:17
专家分:55
注 册:2016-11-28
结帖率:100%
收藏
已结贴  问题点数:4 回复次数:2 
如何设置调用析构函数才能实现以下效果
//////////////如何设置调用析构函数才能实现以下效果:::
Object 1 is created, we've got 1 object(s) now!

Object 2 is created, we've got 2 object(s) now!

Object 3 is created, we've got 3 object(s) now!

Object 3 is deleted, we've got 2 object(s) now!

Object 2 is deleted, we've got 1 object(s) now!////////////这里

Object 4 is created, we've got 2 object(s) now!

Object 4 is deleted, we've got 1 object(s) now!

Object 1 is deleted, we've got 0 object(s) now!//////////到这里

//////////原码:
[code]#include<iostream>
using namespace std;
class Object
{
public:
    Object(int i) {
        id = i;
        count++;
        cout<<"Object "<<id<<" is created, we've got "<<count<<" object(s) now!"<<endl;
    }
    ~Object() {
        count--;
        cout<<"Object "<<id<<" is deleted, we've got "<<count<<" object(s) now!"<<endl;
    }
private:
    int id;
    static int count;
};
int Object::count = 0;


 int main()
{
    Object object[3]{
        Object(1),
        Object(2),
        Object(3)
    };
   
    return 0;
}

[此贴子已经被作者于2017-3-11 16:02编辑过]

搜索更多相关主题的帖子: created Object 如何 
2017-03-10 20:04
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:4 
main
{
    1
    {
        2,3
    }
    4
}
2017-03-11 16:52
luoj75
Rank: 2
等 级:论坛游民
帖 子:17
专家分:55
注 册:2016-11-28
收藏
得分:0 
回复 2楼 rjsp
明白了
O(∩_∩)O谢谢~~~~~~~~
int main()
{
    Object obj1(1);
  { Object obj2(2);
        Object obj3(3);}
        Object obj4(4);
}
2017-03-11 21:46
快速回复:如何设置调用析构函数才能实现以下效果
数据加载中...
 
   



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

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