//没有调用他的构造函数而是调用的拷贝构造函数所以,在调用函数f()时,创建对象没有使object_count++,所以使后来的object_count的数量发生了变化
#include <iostream>
#include <string>
using namespace std;
class howmany{
private:
static int object_count;
public:
howmany()
{
object_count++;
}
~howmany()
{
object_count--;
print("~howmany()");
}
static void print(const char * msg = 0)
{
if(msg)
cout<<msg<<": ";
cout<<"object_count= "
<<object_count <<endl;
}
};
howmany f(howmany x)
{
x.print("x arugment inside f()");
return x;
}
int howmany::object_count = 0;
int main()
{
howmany h;
howmany::print("after construction of h");
howmany h2 = f(h);
howmany::print("after call to f()");
return 0;
}
拷贝构造函数第十章 Thinking inc++
最新推荐文章于 2022-04-19 23:58:03 发布