Topic: How to initialize static data member.


Author: g4165245@nontri.ku.ac.th (Anon Sricharoenchai)
Date: 1998/07/25
Raw View
template <class type>
class test1
{
 public:
  static int int1;
};

template <class type>
int test1<type>::int1=0;

class test2
{
 public:
  static int int1;
};

int test2::int1=0;

void main()
{
 test1<int> Test1;
 test2      Test2;
};

 This program when compile with g++, it has compile time error on
initializing the static member for class test1. The compiler can't
understand the initializing of static member with template. But
initializing of static member for non-template class (test2) is valid.
 How can I initialize the static member of class test1?

 But when I compile with Borland C++ Builder, it has no error. The
latter compiler can implement such initializing of template class.  Which
compiler is right? Do ANSI C++ implement the initializing of static member
for template class?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Dhinakaran V <dina@kiwi.india.TEK.COM>
Date: 1998/07/27
Raw View
Each template class instantiation will get its own static  data member.
Initialize the template class static member as follows:

int test1<int>::int1 = 1;      // for integer instantiations of test1
int test1<float>::int1 = 2;  // for float instantiaions of test1
. . . . ..
. . . . .

I think your g++ compiler is correct.
Bye
                        --dhina





[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/07/27
Raw View
Anon Sricharoenchai wrote:
 >
 > template <class type>
 > class test1
 > {
 >  public:
 >   static int int1;
 > };
 >
 > template <class type>
 > int test1<type>::int1=0;
 >
 > class test2
 > {
 >  public:
 >   static int int1;
 > };
 >
 > int test2::int1=0;
 >
 > void main()
 > {
 >  test1<int> Test1;
 >  test2      Test2;
 > };
 >
 >         This program when compile with g++, it has compile time error on
 > initializing the static member for class test1. The compiler can't
 > understand the initializing of static member with template. But
 > initializing of static member for non-template class (test2) is valid.
 >         How can I initialize the static member of class test1?
 >
 >         But when I compile with Borland C++ Builder, it has no error. The
 > latter compiler can implement such initializing of template class.  Which
 > compiler is right? Do ANSI C++ implement the initializing of static member
 > for template class?

Which version of g++ do you use?
g++-2.8.1 compiles that code just fine.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]