d*********1 发帖数: 25 | 1 why the last one becomes to counter = 1?
//: C05:CountedClass3.cpp
#include
using namespace std;
template class Counted {
static int count;
public:
Counted() { ++count; }
Counted(const Counted&) { ++count; }
~Counted() { --count; }
static int getCount() { return count; }
};
template int Counted::count = 0;
// Curious class definitions
class CountedClass : public Counted {};
class CountedClass2 : public Counted {};
int main() {
Count |
b********n 发帖数: 609 | 2 有什么难懂的,CountedClass2和CountedClass一点关系没有。
【在 d*********1 的大作中提到】 : why the last one becomes to counter = 1? : //: C05:CountedClass3.cpp : #include : using namespace std; : template class Counted { : static int count; : public: : Counted() { ++count; } : Counted(const Counted&) { ++count; } : ~Counted() { --count; }
|
r*********r 发帖数: 3195 | 3 because you have TWO copies of "count":
CountedClass::count, and CountedClass2::count, both initialized to 0 |
T*****9 发帖数: 2484 | 4 CountedClass::i和CountedClass2::i不是一个东西
你可以print出来他们的地址
【在 d*********1 的大作中提到】 : why the last one becomes to counter = 1? : //: C05:CountedClass3.cpp : #include : using namespace std; : template class Counted { : static int count; : public: : Counted() { ++count; } : Counted(const Counted&) { ++count; } : ~Counted() { --count; }
|
d*********1 发帖数: 25 | 5 thanks for all the answers :) they are all right!
due to different parameters, varying classes yield. They are similar to:
class CountedClass : public Counted {};
class CountedClass2 : public Counted {};
as long as type1!=type2, eg int vs double etc. |