z****e 发帖数: 2024 | 1 看到thinking in c++一段代码:
#include
#include
using namespace std;
ofstream out("HowMany.out");
class HowMany {
static int objectCount;
public:
HowMany() { objectCount++; }
static void print(const string& msg = "") {
if(msg.size() != 0) out << msg << ": ";
out << "objectCount = "
<< objectCount << endl;
}
~HowMany() {
objectCount--;
print("~HowMany()");
}
};
int HowMany::objectCount = 0;
HowMany f(HowMany x) {
x.print("x argument inside f()");
return x;
} | t****t 发帖数: 6806 | 2 你定义了HowMany::HowMany(), 没定义HowMany::HowMany(const HowMany&). 对象既可
能从前一个调用生成, 也可能从后一个调用生成. 你计数只计了前面的, 所以看起来拆
掉的比生成的多.
【在 z****e 的大作中提到】 : 看到thinking in c++一段代码: : #include : #include : using namespace std; : ofstream out("HowMany.out"); : class HowMany { : static int objectCount; : public: : HowMany() { objectCount++; } : static void print(const string& msg = "") {
|
|