Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!decwrl!infopiz!lupine!rfg From: rfg@NCD.COM (Ron Guilmette) Newsgroups: comp.lang.c++ Subject: Re: char *const assignment in constructor Message-ID: <4191@lupine.NCD.COM> Date: 2 Mar 91 19:53:16 GMT References: <1991Feb26.195200.8763@urbana.mcd.mot.com> Organization: Network Computing Devices, Inc., Mt. View, CA Lines: 39 In article <1991Feb26.195200.8763@urbana.mcd.mot.com> qbarnes@urbana.mcd.mot.com (Quentin Barnes) writes: >I would like to have a variable of type "char *const" (pointer itself >is const, not what it points to.) However, I would like to delay its >initialization until the contructor is called for its class. Once it >is initialized there, I would like the variable treated as const. > >Is there any way to do this in C++? If you want the "variable" in question to be a *member* of some (containing) class, then the answer to your question is "Yes, this can be done in C++." For example: class C { char *const ccp; public: C (char *arg); }; C::C (char *arg) : ccp(arg) { // ... } Here the data member C::ccp gets initialized when any object of type C is constructed. From that time onwards, the ccp member of each such object is treated as a const value (which cannot be modified). (Note that the thing which ccp points to is *not* considered to be const however.) Not only is this kind of initialization (of const data members) possible, it is actually NECESSARY! As noted in 12.6.3 of the ARM, "This is the only way to initialize nonstatic const and reference members." -- // Ron Guilmette - C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // New motto: If it ain't broke, try using a bigger hammer.