0
Why "112"? HOELP((
template<class T> void f(T){ static int i=0; cout<<++i; } int main(){ f(1); f(1.0); f(1); }
2 ответов
+ 12
Some terms used may be inaccurate, but here goes:
template <class T> defines a T as a template class, a generic data type.
i is a static member of f(T), which means that there is only one instance for each different class defined by T.
When we do :
f(1);
We create an instance of class int. The static member of class int is incremented by 1.
f(1.0);
We create an instance of class double. The static member of class double is incremented by 1.
f(1);
We created another instance of class int. This time, the static member of class int does not get re-initialized. It gets incremented from 1 to 2.
Outputs:
112
0
ohhh thanks