由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - a template counting - anybody understand this?
相关主题
谁给新手解释一下这个c++小程序面试问题一问 (转载)
请教一个C++的编程How to count how much time used for a program
Question about a TICPP example请问:Auto_Ptr、Smart Ptr 和 Reference Counting是什么关系? (转载)
求教一个perl问题MySQL COUNT() 问题.
a very good article about count bitSTL/vector引用成员变量。
GPROF problem一个multithreading 问题
GO game terminal condition请教char *和char []的判断
an interview problem又问几个c语言编程的题目
相关话题的讨论汇总
话题: counted话题: count话题: class
进入Programming版参与讨论
1 (共1页)
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.
1 (共1页)
进入Programming版参与讨论
相关主题
又问几个c语言编程的题目a very good article about count bit
哪位同修能帮我测试一下GPROF problem
问个面试题 (转载)GO game terminal condition
向编程大牛请教一个算法题,谢谢an interview problem
谁给新手解释一下这个c++小程序面试问题一问 (转载)
请教一个C++的编程How to count how much time used for a program
Question about a TICPP example请问:Auto_Ptr、Smart Ptr 和 Reference Counting是什么关系? (转载)
求教一个perl问题MySQL COUNT() 问题.
相关话题的讨论汇总
话题: counted话题: count话题: class