码迷,mamicode.com
首页 > 编程语言 > 详细

没想通的关于复制构造函数C++代码: A obj=func()

时间:2018-12-10 22:05:51      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:cout   return   nbsp   color   复制   UNC   his   col   c++   

class A
{
public:
    A(int x=0):i(x)
    {
        cout << "Normal Contructor: " << i << endl;
    }
    
    ~A()
    {
        cout << "Destructor: " << i << endl;
    }
    
    A(const A & x)
    {
        cout << "Copy Constructor: " << i << endl;
    }
    
    A & operator=(const A & x)
    {
        cout << "Operator = " << endl;
        return *this;
    }


    int i;
    
};

A fun ()
{
    A t(2);
    return t;
}
int main()
{
    A a(1);
    
    A b = fun();
    //fun();
    
    cout << "end of program" << endl;
    
    return 0;
}

  上面main()函数执行后的输出为:

  Normal Contructor: 1
  Normal Contructor: 2
  end of program
  Destructor: 2
  Destructor: 1

int main()
{
    A a(1);
    
    //A b = fun();
    fun();
    
    cout << "end of program" << endl;
    
    return 0;
}
输出为:

Normal Contructor: 1
Normal Contructor: 2
Destructor: 2
end of program
Destructor: 1
 

 

 

 

没想通的关于复制构造函数C++代码: A obj=func()

标签:cout   return   nbsp   color   复制   UNC   his   col   c++   

原文地址:https://www.cnblogs.com/superrunner/p/10099080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!