//: 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&); //成员对成员的拷贝,不是字节对字节的拷贝