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); }

3rd Aug 2017, 6:29 AM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar
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
3rd Aug 2017, 6:58 AM
Hatsy Rei
Hatsy Rei - avatar
0
ohhh thanks
3rd Aug 2017, 7:00 AM
Vladyslav Sabadakh
Vladyslav Sabadakh - avatar