Template Class With Static Data Member

Discussion in 'C++' started by Immortal Nephi, Aug 21, 2009.

  1. 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;

    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;
    }
    Immortal Nephi, Aug 21, 2009
    #1
    1. Advertising

  2. * 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
    Alf P. Steinbach, Aug 21, 2009
    #2
    1. Advertising

  3. Immortal Nephi wrote:
    > 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


    If you want every instance of Test to have its own version of s_data,
    that's exactly what non-static members are for.
    Juha Nieminen, Aug 21, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Payne
    Replies:
    8
    Views:
    445
    Karthik Kumar
    Oct 11, 2004
  2. Siemel Naran
    Replies:
    4
    Views:
    789
    Micah Cowan
    Jan 12, 2005
  3. christopher diggins
    Replies:
    16
    Views:
    735
    Pete Becker
    May 4, 2005
  4. Frederiek
    Replies:
    1
    Views:
    370
    Thomas Tutone
    Sep 14, 2006
  5. dolphin
    Replies:
    3
    Views:
    1,326
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page