免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發新帖
查看: 28532 | 回復: 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
   日韩综合区视频第一页导航,无码JK粉嫩小泬在线观看,午夜精品A片一区二区三区,日日躁夜夜躁狠狠躁麻豆,大胆国模,免费观看无遮挡www的网站