当构造函数或类型转换发生时?

时间:2012-11-19 03:09:23

标签: c++

  

可能重复:
  What are copy elision and return value optimization?

根据昨天提出的问题,我写了一个简单的测试代码:

class A
{
public:
    A(int)
    {
        cout<<"constructor\n";
    }
    A(const A&)
    {
        cout<<"copy constructor\n";
    }
};

int main()
{
    A(0);
    cout<<"------------------------\n";
    A(A(0));
    cout<<"------------------------\n";
    A(A(A(0)));
    cout<<"------------------------\n";
    A(A(A(A(0))));
    cout<<"------------------------\n";
    A(A(A(A(A(0)))));
    cout<<"------------------------\n";
    return 0;
}

输出:

constructor
------------------------
constructor
------------------------
constructor
copy constructor
------------------------
constructor
copy constructor
------------------------
constructor
copy constructor
copy constructor
------------------------

因此,如果发明人打电话或类型演员发生时明确表示非常奇怪,是否有人可以尝试解释这一点?

0 个答案:

没有答案