免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3619 | 回复: 9
打印 上一主题 下一主题

[C++] C++栈里的局部对象在函数返回时不总是自动调用其析构函数 [复制链接]

论坛徽章:
9
程序设计版块每日发帖之星
日期:2016-02-13 06:20:00数据库技术版块每日发帖之星
日期:2016-06-15 06:20:00数据库技术版块每日发帖之星
日期:2016-06-16 06:20:00数据库技术版块每日发帖之星
日期:2016-06-18 06:20:00程序设计版块每日发帖之星
日期:2016-06-27 06:20:00程序设计版块每日发帖之星
日期:2016-07-09 06:20:00IT运维版块每日发帖之星
日期:2016-07-15 06:20:00IT运维版块每日发帖之星
日期:2016-07-27 06:20:00程序设计版块每日发帖之星
日期:2016-08-18 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-05-26 09:52 |只看该作者 |倒序浏览
感觉特坑!

ClxDerived b(); //测试表明没调用析构函数, 也没有调用构造函数!


#include <iostream>
using namespace std;
#include <stdio.h>


class ClxBase
{
public:
    ClxBase() { cout << "constructor base                        Output from the constructor of class base!" << endl; };
    virtual ~ClxBase() { cout << "destructor base                      Output from the destructor of class base!" << endl; };

    void DoSomething() { cout << "DoSomething base                  Do something in class ClxBase!" << endl; };
};

class ClxDerived : public ClxBase
{
        int i;
public:
    ClxDerived(int j) { cout << "constructor derived(with para)                      Output from the constructor of class base!" << endl; i = j;};
        ClxDerived( ) { cout << "constructor derived(void)                     Output from the constructor of class base!" << endl; i = 999999999;};
    ~ClxDerived() { cout << "destructor derived                  Output from the destructor of class ClxDerived!i = " << i << endl;  };

    void DoSomething() { cout << "DoSomething  derived                Do something in class ClxDerived! i = " << i << endl; };
};

//    代码
int abc ()
{
        ClxDerived a(2);
        //ClxDerived b
        ClxDerived b(); //测试表明没调用析构函数, 也没有调用构造函数!
        ClxDerived c;
        ClxBase *pTest = new ClxDerived(3);
        pTest->DoSomething();
        //delete pTest;
        return 0;
}


int main ()
{
        abc ();
        printf ("heheh\r\n");
}



[root@com2 wfb]# g++ t.c -o t
[root@com2 wfb]# ./t
constructor base                        Output from the constructor of class base!
constructor derived(with para)                      Output from the constructor of class base!
constructor base                        Output from the constructor of class base!
constructor derived(void)                     Output from the constructor of class base!
constructor base                        Output from the constructor of class base!
constructor derived(with para)                      Output from the constructor of class base!
DoSomething base                  Do something in class ClxBase!
destructor derived                  Output from the destructor of class ClxDerived!i = 999999999
destructor base                      Output from the destructor of class base!
destructor derived                  Output from the destructor of class ClxDerived!i = 2
destructor base                      Output from the destructor of class base!
heheh

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
2 [报告]
发表于 2016-05-26 09:57 |只看该作者
因为这不是定义了一个ClxDerived变量b

论坛徽章:
9
程序设计版块每日发帖之星
日期:2016-02-13 06:20:00数据库技术版块每日发帖之星
日期:2016-06-15 06:20:00数据库技术版块每日发帖之星
日期:2016-06-16 06:20:00数据库技术版块每日发帖之星
日期:2016-06-18 06:20:00程序设计版块每日发帖之星
日期:2016-06-27 06:20:00程序设计版块每日发帖之星
日期:2016-07-09 06:20:00IT运维版块每日发帖之星
日期:2016-07-15 06:20:00IT运维版块每日发帖之星
日期:2016-07-27 06:20:00程序设计版块每日发帖之星
日期:2016-08-18 06:20:00
3 [报告]
发表于 2016-05-26 10:11 |只看该作者
hellioncu 发表于 2016-05-26 09:57
因为这不是定义了一个ClxDerived变量b



ClxDerived b(); //测试表明没调用析构函数, 也没有调用构造函数!

那这既然没有构造对象,那到底是干啥了?

论坛徽章:
324
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
4 [报告]
发表于 2016-05-26 10:16 |只看该作者
mordorwww 发表于 2016-05-26 10:11
ClxDerived b(); //测试表明没调用析构函数, 也没有调用构造函数!

那这既然没有构造对象,那到底 ...


函数申明

论坛徽章:
3
15-16赛季CBA联赛之佛山
日期:2016-11-04 14:21:2015-16赛季CBA联赛之山西
日期:2017-01-05 21:29:2715-16赛季CBA联赛之佛山
日期:2017-07-28 16:27:15
5 [报告]
发表于 2016-05-26 16:18 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
9
程序设计版块每日发帖之星
日期:2016-02-13 06:20:00数据库技术版块每日发帖之星
日期:2016-06-15 06:20:00数据库技术版块每日发帖之星
日期:2016-06-16 06:20:00数据库技术版块每日发帖之星
日期:2016-06-18 06:20:00程序设计版块每日发帖之星
日期:2016-06-27 06:20:00程序设计版块每日发帖之星
日期:2016-07-09 06:20:00IT运维版块每日发帖之星
日期:2016-07-15 06:20:00IT运维版块每日发帖之星
日期:2016-07-27 06:20:00程序设计版块每日发帖之星
日期:2016-08-18 06:20:00
6 [报告]
发表于 2016-05-27 08:49 |只看该作者
本帖最后由 mordorwww 于 2016-05-27 09:36 编辑

同笑

论坛徽章:
12
2015年辞旧岁徽章
日期:2015-03-03 16:54:1515-16赛季CBA联赛之同曦
日期:2017-03-17 19:13:162016科比退役纪念章
日期:2016-11-07 08:28:12luobin
日期:2016-06-17 17:46:36wusuopu
日期:2016-06-17 17:43:4515-16赛季CBA联赛之福建
日期:2016-01-14 12:49:22程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:00程序设计版块每日发帖之星
日期:2015-06-08 22:20:002015年亚洲杯之科威特
日期:2015-03-24 14:21:272015年迎新春徽章
日期:2015-03-04 09:57:092016科比退役纪念章
日期:2018-04-10 16:20:18
7 [报告]
发表于 2016-05-27 09:31 |只看该作者
同笑

论坛徽章:
0
8 [报告]
发表于 2016-05-27 12:12 |只看该作者
这是声明了一个函数,老早之前见过这个坑了

论坛徽章:
2
技术图书徽章
日期:2014-04-15 16:30:27金牛座
日期:2014-06-06 16:20:49
9 [报告]
发表于 2016-09-30 15:11 |只看该作者
现在版本高一点的编译器,好像是有告警或者报错了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP