Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template Class With Static Data Member

Reply
Thread Tools

Template Class With Static Data Member

 
 
Immortal Nephi
Guest
Posts: n/a
 
      08-21-2009
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;
}
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-21-2009
* 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
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      08-21-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
template class static template type member address allocation Frederiek C++ 1 09-14-2006 03:10 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM



Advertisments