26.拷贝构造I

//: C11: HowMany.cpp
#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;

class HowMany
{
  public:
    HowMany() { objectCount++; print("HowMany()"); }
    void print(const string& msg = "")
    {
       if(msg.size()!=0)
         cout << msg << ": ";
       cout << "objectCount = " << objectCount << endl;
    }
    ~HowMany()
    {
      objectCount--;
      print("~HowMany()");
    }
};

//Pass and return BY VALUE
HowMany f(HowMany x)
{
  cout << "begin of f" << endl;
  x.print("x argument inside f()");
  cout << "end of f" << endl;
  return x;
}

int main()
{
  HowMany h; //调用构造函数 HowMany():objectCount = 1
  h.print("after construction of h"); //after construction of h: objectCount = 1
  HowMany h2 = f(h); //使用f(h)时候 
                     //begin of f
                     //x argument inside f():objectCount = 1
                     //end of f
                     //HowMany h2调用构造函数
                     //~HowMany():objectCount = 0

  h.print("after call to f()"); //after call to f():objectCount = 0
                                //调用析构函数
                                //~HowMany():objectCount = -1
                                //~HowMany():objectCount = -2
}
#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;

class HowMany
{
  public:
    HowMany() { objectCount++; print("HowMany()"); }
    void print(const string& msg = "")
    {
       if(msg.size()!=0)
         cout << msg << ": ";
       cout << "objectCount = " << objectCount << endl;
    }
    ~HowMany()
    {
      objectCount--;
      print("~HowMany()");
    }
};

//Pass and return BY VALUE
HowMany f(HowMany x)
{
  cout << "begin of f" << endl;
  x.print("x argument inside f()");
  cout << "end of f" << endl;
  return x;
}

int main()
{
  HowMany h; //调用构造函数 HowMany():objectCount = 1
  h.print("after construction of h"); //after construction of h: objectCount = 1
  HowMany h2 = h; //这里构造了一个h2出来,但是这个构造没有经过构造函数.                   
  h.print("after call to f()"); //after call to f():objectCount = 1
                                //调用析构函数
                                //~HowMany():objectCount = 0
                                //~HowMany():objectCount = -1
}
#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;

class HowMany
{
  public:
    HowMany() { objectCount++; print("HowMany()"); }
    HowMany(int i) { objectCount++; print("HowMany(int)"); }
    void print(const string& msg = "")
    {
       if(msg.size()!=0)
         cout << msg << ": ";
       cout << "objectCount = " << objectCount << endl;
    }
    ~HowMany()
    {
      objectCount--;
      print("~HowMany()");
    }
};

//Pass and return BY VALUE
HowMany f(HowMany x)
{
  cout << "begin of f" << endl;
  x.print("x argument inside f()");
  cout << "end of f" << endl;
  return x;
}

int main()
{
  HowMany h; //调用构造函数 HowMany():objectCount = 1
  h.print("after construction of h"); //after construction of h: objectCount = 1
  HowMany h2(10); //HowMany(int):objectCount = 2
  //也可写为HowMany h2 = 10;
  h.print("after call to f()"); //after call to f():objectCount = 2
                                //调用析构函数
                                //~HowMany():objectCount = 1
                                //~HowMany():objectCount = 0
}
#include <iostream>
#include <string>
using namespace std;

static int objectCount = 0;

class HowMany
{
  public:
    HowMany() { objectCount++; print("HowMany()"); }
    HowMany(int i) { objectCount++; print("HowMany(int)"); }
    HowMany(const HowMany& o) { objectCount++; print("HowMany(HM)");}
    void print(const string& msg = "")
    {
       if(msg.size()!=0)
         cout << msg << ": ";
       cout << "objectCount = " << objectCount << endl;
    }
    ~HowMany()
    {
      objectCount--;
      print("~HowMany()");
    }
};

//Pass and return BY VALUE
HowMany f(HowMany x)
{
  cout << "begin of f" << endl;
  x.print("x argument inside f()");
  cout << "end of f" << endl;
  return x;
}

int main()
{
  HowMany h; //调用构造函数 HowMany():objectCount = 1
  h.print("after construction of h"); //after construction of h: objectCount = 1
  HowMany h2 = h; //HowMany(int):objectCount = 2
  h.print("after call to f()"); //after call to f():objectCount = 2
                                //调用析构函数
                                //~HowMany():objectCount = 1
                                //~HowMany():objectCount = 0
}

int main()
{
  HowMany h; //调用构造函数 HowMany():objectCount = 1
  h.print("after construction of h"); //after construction of h: objectCount = 1
  HowMany h2 = f(h); //f里面的x,h2
  //这种写法相当于HowMany h2(f(h)).所以f里面的x需要调用构造函数HowMany(HM).
  //HowMany(HM):objectCount = 2
  //begin of f
  //x argument inside f():objectCount = 2
  //end of f
  //接着h2调用构造函数
  //HowMany(HM):onjectCount = 3  
 //~HowMany():objectCount = 2
  h.print("after call to f()"); //after call to f():objectCount = 2
                                //调用析构函数
                                //~HowMany():objectCount = 1 
                                //~HowMany():objectCount = 0
}

拷贝构造

T::T(const T&); //成员对成员的拷贝,不是字节对字节的拷贝

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值