Path: utzoo!attcan!uunet!zaphod.mps.ohio-state.edu!usc!cs.utexas.edu!yale!quasi-eli!cs.yale.edu!starr-page From: starr-page@cs.yale.edu (Page Starr) Newsgroups: comp.lang.c++ Subject: Re: Constant member variables in TC++ Message-ID: <1990Nov11.135118@cs.yale.edu> Date: 11 Nov 90 18:51:17 GMT References: Sender: news@cs.yale.edu Reply-To: starr-page@cs.yale.edu (Page Starr) Organization: Yale University Computer Science Dept., New Haven, CT 06520-2158 Lines: 44 Nntp-Posting-Host: sluggo.na.cs.yale.edu In article , ahuttune@niksula.hut.fi (Ari Juhani Huttunen) writes: |> An example class: |> |> class X { |> const int const_int; |> }; |> |> Gives a warning: constant field not initialized. |> |> class X { |> const int const_int = 5; |> }; |> |> Gives an error: can't initialize a field. |> |> So??????? |> -- constant members in classes are initialized in the member initialization list. So the correct solution here would be: class X { const int const_int; X(): const_int(5) {;} }; If you don't want a constant in each object, the correct solution would be: class X2{ static const int const_int2; }; and then in one .C file: const int X2::const_int2 = 5; ------------------------------------------------------------------------ ------ CSnet: starr@cs.yale.edu BITNET: starr@yalecs.BITNET ARPAnet: starr%cs.yale.edu@relay.cs.net Phone: (203)432-1215 USmail: Page Starr, Box 2158 Yale Station, New Haven, CT 06520