返回时为什么本地对象不会破坏

时间:2015-01-09 04:34:54

标签: c++

我无法理解以下测试程序的输出,为什么在incDate()结束时不会破坏d1。为什么不调用复制构造函数。

输出是,

constructing 0xbf9be858

constructing 0xbf9be85c

Marker

destructing 0xbf9be85c

destructing 0xbf9be858

测试程序是,

class Date
{
    int days;
public:
    Date();
    Date(const Date&);
    Date incDate();
    ~Date(){cout<<"destructing "<<this<<endl;}
};

Date::Date()
{
    cout<<"constructing " << this <<endl;
    days=0;
}

Date::Date(const Date& aDate)
{
    cout<<"copy constructing " << this <<endl;
    days = aDate.days;
}

Date Date::incDate()
{
    Date d1;
    d1.days++;
    return d1;
}

int main()
{
    Date d1;

    Date d2 = d1.incDate();
    cout<<"Marker"<<endl;
}

0 个答案:

没有答案