亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2431 | 回復: 4
打印 上一主題 下一主題

[C++] 運算符重載 成員函數里的 局部對象在函數退出時為何未調用對象析構 函數 [復制鏈接]

論壇徽章:
9
程序設計版塊每日發(fā)帖之星
日期:2016-02-13 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-15 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-16 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-18 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-06-27 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-07-09 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-07-15 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-07-27 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-08-18 06:20:00
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2017-03-15 20:08 |只看該作者 |倒序瀏覽
Useless Useless:perator+(const Useless & f)const
{
    cout << "Entering operator+()\n";
    Useless temp = Useless(n + f.n);
    Useless hhh(10, 'd');
       
        temp.aa = 'b';
    for (int i = 0; i < n; i++)
        temp.pc = pc;
    for (int i = n; i < temp.n; i++)
        temp.pc = f.pc[i - n];
    cout << "operator+ temp object:" << " num "<< n << "\n";
    cout << "operator+ Leaving operator+()"<< " num "<< n << endl;
        hhh.aa = 'f';
        cout << "operator+ hhh aa = " << hhh.aa << endl;
    return temp;
}




// useless.cpp -- an otherwise useless class with move semantics
#include <iostream>
using namespace std;

// interface
class Useless
{
//private:
public:
    char aa = 'a';
    int n;          // number of elements
    char * pc;      // pointer to data
    static int ct;  // number of objects
    void ShowObject() const;
public:
    Useless();
    explicit Useless(int k);
    Useless(int k, char ch);
    Useless(const Useless & f); // regular copy constructor
    Useless(Useless && f);      // move constructor
    ~Useless();
    Useless operator+(const Useless & f)const;
// need operator=() in copy and move versions
    void ShowData() const;
};

// implementation
int Useless::ct = 0;

Useless::Useless()
{
    ++ct;
    n = 0;
    pc = nullptr;
    cout << "default constructor called; number of objects: " << ct << endl;
    ShowObject();
}

Useless::Useless(int k) : n(k)
{
    ++ct;
    cout << "int constructor called; number of objects: " << ct << endl;
    pc = new char[n];
    ShowObject();
}

Useless::Useless(int k, char ch) : n(k)
{
    ++ct;
    cout << "int, char constructor called; number of objects: " << ct << endl;
    pc = new char[n];
    for (int i = 0; i < n; i++)
        pc = ch;
    ShowObject();
}

Useless::Useless(const Useless & f): n(f.n)
{
    ++ct;
    cout << "copy const called; number of objects: " << ct << endl;
    pc = new char[n];
    for (int i = 0; i < n; i++)
        pc = f.pc;
    ShowObject();
}

Useless::Useless(Useless && f): n(f.n)
{
    ++ct;
    cout << "move constructor called; number of objects: " << ct << endl;
    pc = f.pc;       // steal address
    f.pc = nullptr;  // give old object nothing in return
    f.n = 0;
    ShowObject();
}

Useless::~Useless()
{
    //cout << "destructor called; objects left: " << --ct << " num "<< n << endl;
    cout << "****************deleted object:aa = " << aa << endl;
    //ShowObject();
        //ShowData();
    delete [] pc;
}

Useless Useless:perator+(const Useless & f)const
{
    cout << "Entering operator+()\n";
    Useless temp = Useless(n + f.n);
    Useless hhh(10, 'd');
       
        temp.aa = 'b';
    for (int i = 0; i < n; i++)
        temp.pc = pc;
    for (int i = n; i < temp.n; i++)
        temp.pc = f.pc[i - n];
    cout << "operator+ temp object:" << " num "<< n << "\n";
    cout << "operator+ Leaving operator+()"<< " num "<< n << endl;
        hhh.aa = 'f';
        cout << "operator+ hhh aa = " << hhh.aa << endl;
    return temp;
}

void Useless::ShowObject() const
{
    cout << "Number of elements: " << n;
    cout << " Data address: " << (void *) pc << endl;
}

void Useless::ShowData() const
{


        Useless hhh(10, 'a');
        hhh.aa = 'c';
        cout << "ShowData aa = " << aa << endl;

        /*
    if (n == 0)
        cout << "(object empty)";
    else
        for (int i = 0; i < n; i++)
            cout << pc;
    cout << endl;
    */
}

// application
int ttt()
{
    {
        Useless one(10, 'x');
        Useless two = one;          // calls copy constructor
        Useless three(20, 'o');
        Useless four(one + three);  // calls operator+(), move constructor
        cout << "moved ok\n";
        cout << "object one: " << one.n << endl;
        one.ShowData();
        cout << "object two: " << two.n << endl;
        two.ShowData();
        cout << "object three: " << three.n << endl;
        three.ShowData();
        cout << "object four: " << four.n << endl;
        four.ShowData();
    }
    // cin.get();
}

int main()
{
    ttt();
        cout << "main exiting: " << endl;
}

論壇徽章:
9
程序設計版塊每日發(fā)帖之星
日期:2016-02-13 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-15 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-16 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-18 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-06-27 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-07-09 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-07-15 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-07-27 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-08-18 06:20:00
2 [報告]
發(fā)表于 2017-03-15 20:11 |只看該作者
媽的 ,調 了
問題是return出去的 那個 局部對象 未調用析構函數 。

論壇徽章:
9
程序設計版塊每日發(fā)帖之星
日期:2016-02-13 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-15 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-16 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-18 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-06-27 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-07-09 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-07-15 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-07-27 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-08-18 06:20:00
3 [報告]
發(fā)表于 2017-03-15 20:14 |只看該作者
本帖最后由 mordorwww 于 2017-03-15 20:17 編輯

不做 右值引用時才析構,否則不析構 。也就是:
Useless four(one + three);  //不析構 // calls operator+(), move constructor
one + three; //析構



問題是return的 對象保存在哪里 ?

論壇徽章:
9
程序設計版塊每日發(fā)帖之星
日期:2016-02-13 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-15 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-16 06:20:00數據庫技術版塊每日發(fā)帖之星
日期:2016-06-18 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-06-27 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-07-09 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-07-15 06:20:00IT運維版塊每日發(fā)帖之星
日期:2016-07-27 06:20:00程序設計版塊每日發(fā)帖之星
日期:2016-08-18 06:20:00
4 [報告]
發(fā)表于 2017-03-15 20:26 |只看該作者
本帖最后由 mordorwww 于 2017-03-15 20:31 編輯

和右值引用  move構造都沒關系

只要是 要 return的 局部對象變量 都不會析構,貌似都 只有 一分 啊,根本不需要右值引用和move構造就 可以避免拷貝?
c++就 不存在 把返回的 局部對象拷貝 到臨時區(qū),再拷貝給 caller?

論壇徽章:
14
巨蟹座
日期:2013-11-19 14:09:4615-16賽季CBA聯(lián)賽之青島
日期:2016-07-05 12:36:0515-16賽季CBA聯(lián)賽之廣東
日期: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
5 [報告]
發(fā)表于 2017-03-16 08:39 |只看該作者
調用了呀,“****************deleted object:aa = f” 就是 hhh 析構產生的。
而 temp,因為 RVO 的要求,它其實就是 four。不行你輸出它倆的地址看看,是一樣的。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP