Path: utzoo!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!rutgers!att!westmark!mole-end!mat From: mat@mole-end.UUCP (Mark A Terribile) Newsgroups: comp.lang.c++ Subject: Re: Static consts within classes? Summary: Should work ... Keywords: static const class Message-ID: <419@mole-end.UUCP> Date: 28 Jun 90 02:45:03 GMT References: <10460@batcomputer.tn.cornell.edu> Distribution: comp Organization: mole-end--private system. admin: mole-end!newtnews Lines: 49 > Consider the following short piece of code. ... > class X { // error codes for system. > . . . > static int my_error_3; > . . . > }; > . . . > int X::my_error_1 = 1; > ... I am trying to wrap generic > constants in a class, from which they will be available to clients via > inheritance. ... The code ... does basically what I want, except [that] > the member functions of class Y can change the value of the statics in > class X. What I would like to do is have the members of class X be static > consts. ... That's just fine. Inside the class, write static const int my_error_3; > ... That way, there will only be one copy of the values around in the > system, and they cannot be altered. ... Weeeellll ... given pointer misfeasance they can be altered, and although there is only one copy of each, there may be many copies of the addressing operations that access them. (There is a good argument here for allowing the initializer for a static member const to be written in the class header.) > system, and they cannot be altered. Yet I don't see how to do this. ... In just about the way you are doing it, except that ... > ... Seems I can define them as static to get only a single copy, which has > the problem that the values can be changed, or I can declare them to be > consts, with an appropriate constructor, but then I have multiple copies of > the consts. ... ... you must put the initialization in a program file instead of a header file. You should (at least w/AT&T's 2.0) get a single copy of that variable, initialized in just one place, and visible as a member of the class scope, either by member selection or by qualification. -- (This man's opinions are his own.) From mole-end Mark Terribile