Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!fernwood!dumbcat!marc From: marc@dumbcat.sf.ca.us (Marco S Hyman) Newsgroups: comp.lang.c++ Subject: Re: Constant member variables in TC++ Message-ID: <227@dumbcat.sf.ca.us> Date: 11 Nov 90 21:59:10 GMT References: <1990Nov11.181752.8098@athena.mit.edu> Organization: MH Software, Hayward, Ca. Lines: 48 In article <1990Nov11.181752.8098@athena.mit.edu> bjaspan@athena.mit.edu (Barr3y Jaspan) writes: In article , ahuttune@niksula.hut.fi (Ari Juhani Huttunen) writes: |> class X { |> const int const_int; |> }; |> |> Gives a warning: constant field not initialized. |> [but initializing it...] |> |> Gives an error: can't initialize a field. Anyway, the only way I know of to get around this problem is to declare an enum with the constant you want. It's a kludge but it works. So, for example, class X { enum { const_int = 10 }; int by_the_way_this_works_too[const_int]; }; will make "const_int" be a constant 10 and even declares a constant-length array in the process (which there is also no other way to do). Note that enums do not have to be named. Uh, this is the hard way. The requirement is that the constant members of a class be initialized when an object is created. There is a syntax to support this. Try: class X { private: const int const_int; public: X(int i) : const_int(i) {}; }; This compiles with cfront 2.0 without error. See section 12.6 of the ARM, especially the example on page 290. // marc -- // marc@dumbcat.sf.ca.us // {ames,decwrl,sun}!pacbell!dumbcat!marc