由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - One C++ question
相关主题
说说我被面试到的c++题目吧包子呼唤大牛--问关于C++Destructor的问题 (转载)
c++ class default functions?问个C++重新编译的问题
BB phone interview questionC++面试问题,高人请进啊~~~
C++ online Test 一题请教1个工作面试题
弱问:singleton要不要destructor啊?为什么C++的constructor出错可以抛出异常,而destructor出错
C++ Q47: protected constructor (C39)面经: bloomberg 电面
关于singleton 的面试题C++ Q65: recompiling (IB)
新Qualcomm面经C++相关的面经
相关话题的讨论汇总
话题: static话题: int话题: class话题: c++话题: question
进入JobHunting版参与讨论
1 (共1页)
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.
1 (共1页)
进入JobHunting版参与讨论
相关主题
C++相关的面经弱问:singleton要不要destructor啊?
小公司web server面经C++ Q47: protected constructor (C39)
准备去BB onsite 了,求bless (undate)关于singleton 的面试题
发发面经 攒人品 C++的新Qualcomm面经
说说我被面试到的c++题目吧包子呼唤大牛--问关于C++Destructor的问题 (转载)
c++ class default functions?问个C++重新编译的问题
BB phone interview questionC++面试问题,高人请进啊~~~
C++ online Test 一题请教1个工作面试题
相关话题的讨论汇总
话题: static话题: int话题: class话题: c++话题: question