* Immortal Nephi:
> Can you create two objects of template class so template class can
> have its own copy of static data member instead of shared static data
> member?
>
> template< typename T >
> class Test
> {
> public:
> Test() {}
> ~Test() {}
>
> static T s_data;
> };
>
> template< typename T >
> T Stack< T >::s_data = 0;
The "Stack<T>::" seems to have escaped from your real code; for the code
presented it should be "Test::".
> int main()
> {
> Test< long int > a;
> Test< short int > b;
>
> a.s_data = 1; // OK...Own one copy of static data member
> b.s_data = 2; // OK...Own one copy of static data member
>
> Test< long int > c;
>
> c.s_data = 3; // Not OK because static data member is shared by a and
> c.
>
> return 0;
> }
You have two options: (A) make s_data non-static, or (B) add at least one more
template parameter.
You do not explain what your goal is, but it seems likely that (A) is the best
choice anyway.
Cheers & hth.,
- Alf
|