拷贝构造函数&构造函数&重载赋值运算符

  1. class CTest
  2. {
  3. public:
  4. CTest(int iValue)
  5. {
  6. m_iValue = iValue;
  7. cout<<"Constructor Is Callled."<<endl;
  8. }
  9. ~CTest()
  10. {
  11. }
  12. CTest(CTest const& t)
  13. {
  14. m_iValue = t.m_iValue;
  15. cout<<"Copy Constructor Is Called."<<endl;
  16. }
  17. CTest& operator=(CTest const& test)
  18. {
  19. cout<<"重载赋值运算符."<<endl;
  20. m_iValue = test.m_iValue;
  21. return *this;
  22. }
  23. int GetValue() const
  24. {
  25. return m_iValue;
  26. }
  27. private:
  28. int m_iValue;
  29. };
  30. void printTest(CTest t)
  31. {
  32. cout<<t.GetValue()<<endl;
  33. }
  34. int main()
  35. {
  36. CTest t1(3);
  37. CTest t2(t1);
  38. t2 = t1; //会调用赋值构造函数
  39. CTest t3 = t1;//会调用拷贝构造函数
  40. printTest(t1); //会调用拷贝构造函数(和CTest t=t1 效果是一样的)
  41. return 0;
  42. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值