首页 > 编程语言 > 详细

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

时间:2018-12-10 21:49:00      阅读:202      评论:0      收藏:0      [点我收藏+]
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()

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!