[comp.lang.c++] Initialization of static class members?

ian@dms.cdc.com (Ian Hogg) (09/26/90)

The following compiles and executes with g++.  It does not compile with HP's 
C++ or Oregon Software C++ 2.0.

class butthead {
        .
	.
	.
  static int n = 2;
	 int c = 1;
        .
	.
	.
};


Which compiler(s) is correct?  I find it a big pain to have to explicity
initialize n & c for every constructor I may have.

The big problem is with the static member.  I would like this initialized
exactly once not every time an instance of butthead is created.

Please respond directly via e-mail since I don't have news set up 100% right
now.
-- 
   Ian Hogg	x-4484

shankar@hpclscu.HP.COM (Shankar Unni) (10/02/90)

> class butthead {
>   static int n = 2;
> 	 int c = 1;
> };
> 
> 
> Which compiler(s) is correct?  I find it a big pain to have to explicity
> initialize n & c for every constructor I may have.

This syntax is an extension to the AT&T language spec, specifically added
by g++ only.

The "official way" to initialize a static member is not in the declaration,
and not in the constructor, but in the actual definition.

A static member *must* have an actual externally-visible definition
*outside* the class body, in one compilation unit only, as in:

   // butthead.h
   
   class butthead {
      static int n;
   };
   
   // butthead.C
   
   extern int butthead::n = 0;	// definition.
   

As to initializing non-static members, use the member-initialization
syntax, as in:

   class butthead {
      int n;
      butthead() : n(0) { }
   };

Yes, you'll have to specify the initializer in each of the constructors.
-----
Shankar Unni                                   E-Mail: 
Hewlett-Packard California Language Lab.     Internet: shankar@hpda.hp.com
Phone : (408) 447-5797                           UUCP: ...!hplabs!hpda!shankar

des@amara.uucp (Dave Steinhoff) (10/03/90)

In article <58170034@hpclscu.HP.COM> shankar@hpclscu.HP.COM (Shankar Unni) writes:

>>   The "official way" to initialize a static member is not in the declaration,
>>   and not in the constructor, but in the actual definition.
>>
>>   A static member *must* have an actual externally-visible definition
>>   *outside* the class body, in one compilation unit only, as in:
>>
>>      // butthead.h
>>
>>      class butthead {
>>	 static int n;
>>      };
>>
>>      // butthead.C
>>
>>      extern int butthead::n = 0;	// definition.
        ^^^^^^

The "extern" specifier is wrong.  The definition must appear in exactly
one file, but since the static member is effectively a global, it must
not be extern.  See ARM, pp 180-181.

Dave
--
-------------------------------------------------------------------
Dave Steinhoff                              Applied Dynamics, Int'l
des@amara.UUCP                              3800 Stone School Rd.
des@adi.com                                 Ann Arbor, Mi 48108
...uunet!amara!des                          (313)973-1300                 
-------------------------------------------------------------------