home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18504 < prev    next >
Encoding:
Text File  |  1992-12-29  |  2.0 KB  |  71 lines

  1. Xref: sparky comp.lang.c++:18504 gnu.g++.help:1617
  2. Newsgroups: comp.lang.c++,gnu.g++.help
  3. Path: sparky!uunet!cs.utexas.edu!torn!skule.ecf!drill.me!zougas
  4. From: zougas@me.utoronto.ca (Tom Zougas)
  5. Subject: static member initialization
  6. Message-ID: <C01An8.2E1@me.utoronto.ca>
  7. Sender: news@me.utoronto.ca (News Reader)
  8. Organisation: U of Toronto, Dept. of Mechanical Engineering
  9. Organization: UofT Mechanical Engineering
  10. Distribution: comp
  11. Date: Tue, 29 Dec 1992 18:42:44 GMT
  12. Lines: 57
  13.  
  14. I'm using gcc-2.3.2 on a Sparc, Sun OS4.1.1 and am having difficulties
  15. with the following situation:
  16.  
  17. I declare a template class which contains a static member. When I try
  18. to initialize the static member, it doesn't work unless I do it inside
  19. the scope of 'main'. Static members of non-template classes can be
  20. initialized outside the scope of 'main' and I would expect the same
  21. for template classes. Is this how template classes work or is it a
  22. bug in the gcc compiler? Please e-mail any responses. Thanks.
  23.  
  24. The following is the test code I used:
  25.  
  26. // beginning of included file: test.cc
  27. ////////////////////////////////////////////////////////////////
  28. #include <iostream.h>
  29.  
  30. class Int {
  31. public:
  32.   Int( int i ) { _i = i; }
  33.   void print() { cout << _i << endl; }
  34.   int _i;
  35. };
  36.  
  37. class _Test {
  38. public:
  39.   _Test() {}
  40.   void print() { x.print(); }
  41.   static Int x;
  42. };
  43.  
  44. template <class Type>
  45. class Test : public _Test {
  46. public:
  47.   void print() { _Test::print(); x.print(); }
  48.   static Int x;
  49. };
  50.  
  51. Int _Test::x(99);
  52. //Int Test<int>::x(88);    // <=== this is where I want to initialize
  53.             //    but doesn't work
  54.  
  55. main()
  56. {
  57.   Int Test<int>::x(88);    // <=== this is where I have to initialize
  58.             //    to get it to work
  59.   Test<int> ti;
  60.   ti.print();
  61.   test();
  62. }
  63.  
  64. ////////////////////////////////////////////////////////////////
  65. // end of included file
  66. -- 
  67. ===========================================================================
  68. Tom Zougas                          zougas@me.utoronto.ca
  69. Engineering Mechanics and Design Laboratory               416-978-1281
  70. Dept of Mechanical Engineering, University of Toronto,      Toronto, CANADA
  71.