免费注册 查看新帖 |

Chinaunix

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

[C++] 请大家帮忙分析一下,这两段程序的运行过程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2020-10-12 14:58 |只看该作者 |倒序浏览
include <iostream>
#include <stdio.h>
using namespace std;

class Cexample
{
        private:
                int a;
        public:
                Cexample(int b)
                {
                        a = b;
                        printf("constructor is callec\n");
                }
        Cexample(const Cexample & c)
        {
                a = c.a;
                printf("copy constructor is called\n");
        }
        ~Cexample()
        {
                cout << "destructor is called\n";
        }
        void show()
        {
                cout << a << endl;
        }
};
Cexample g_fun(Cexample c)
{
        cout << "g_func"<<endl;
}

int main()
{
        Cexample A(100);
Cexample B=A;
        B.show();
        g_fun(A);
        return 0;
}
运行结果:
constructor is callec
copy constructor is called
100
copy constructor is called
g_func
destructor is called
destructor is called
destructor is called
destructor is called
原文只有三个destructor is called,我运行的为啥有4个,疑问?

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

class Cexample
{
        private:
                int a;
        public:
                Cexample(int b)
                {
                        a = b;
                        printf("constructor is callec\n");
                }
        Cexample(const Cexample & c)
        {
                a = c.a;
                printf("copy constructor is called\n");
        }
        ~Cexample()
        {
                cout << "destructor is called\n";
        }
        void show()
        {
                cout << a << endl;
        }
};
Cexample g_fun()
{
        Cexample temp(0);
        return temp;
}

int main()
{
        g_fun();
        return 0;
}
运行结果:
constructor is callec
destructor is called
原文运行结果:
constructor is callec
copy constructor is called
destructor is called
destructor is called
怎么和原文:
https://www.cnblogs.com/alantu2018/p/8459250.html
运行结果很不同呢,请各位帮忙看一下!谢谢!

论坛徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16赛季CBA联赛之青岛
日期:2016-07-05 12:36:0515-16赛季CBA联赛之广东
日期:2016-06-29 11:45:542015亚冠之全北现代
日期:2015-07-22 08:09:472015年辞旧岁徽章
日期:2015-03-03 16:54:15巨蟹座
日期:2014-12-29 08:22:29射手座
日期:2014-12-05 08:20:39狮子座
日期:2014-11-05 12:33:52寅虎
日期:2014-08-13 09:01:31巳蛇
日期:2014-06-16 16:29:52技术图书徽章
日期:2014-04-15 08:44:01天蝎座
日期:2014-03-11 13:06:45
2 [报告]
发表于 2020-10-13 21:30 |只看该作者
Cexample g_fun(Cexample c)
{
        cout << "g_func"<<endl;
}
你的return语句在哪儿?

Cexample g_fun()
{
        Cexample temp(0);
        return temp;
}
按标准,这里可以进行 具名返回值优化,也就是直接在调用处构造对象。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP