p*********r 发帖数: 48 | 1 How to count the number of instances for all classes? One way is to add a
static int field to every class and increase this static int by one in all
constructors and the assignment operator and decease it by one in the
destructor. The interviewer wants to know if there is any way to avoid
adding a static int to every class? |
s*********t 发帖数: 1663 | 2 add a static int to global?
【在 p*********r 的大作中提到】 : How to count the number of instances for all classes? One way is to add a : static int field to every class and increase this static int by one in all : constructors and the assignment operator and decease it by one in the : destructor. The interviewer wants to know if there is any way to avoid : adding a static int to every class?
|
p*********r 发帖数: 48 | 3 Let me clarify the question. He wants to count the number of instances for
each class. So global int does not work.
【在 s*********t 的大作中提到】 : add a static int to global?
|
p*********r 发帖数: 48 | 4 I think I get the answer now. Use a template, private inheritance and
multiple inheritance. |
h**k 发帖数: 3368 | 5 所有class继承一个计数器class? 那你还是要在那个class里声明一个静态变量。
【在 p*********r 的大作中提到】 : I think I get the answer now. Use a template, private inheritance and : multiple inheritance.
|
p*********r 发帖数: 48 | 6 只需在计数器class template里声明一个静态变量
【在 h**k 的大作中提到】 : 所有class继承一个计数器class? 那你还是要在那个class里声明一个静态变量。
|
z****e 发帖数: 2024 | 7 i think we can keep a reference counter such like
private:
int* ref;
Just like shared_ptr does.
what do you think?
Thanks.
【在 p*********r 的大作中提到】 : How to count the number of instances for all classes? One way is to add a : static int field to every class and increase this static int by one in all : constructors and the assignment operator and decease it by one in the : destructor. The interviewer wants to know if there is any way to avoid : adding a static int to every class?
|
a****n 发帖数: 1887 | 8 using template to generate concrete class with static counter |
f**********w 发帖数: 93 | 9 We can use factory, and keep track number of objects created. |