From: ryot AT bigfoot DOT com (George Ryot) Newsgroups: comp.os.msdos.djgpp Subject: Re: c++ const definition in djgpp 2.95 problem Message-ID: <37c1c55c.147383@news.clara.net> References: <37c18ffb DOT 1378453 AT news DOT kfunigraz DOT ac DOT at> X-Newsreader: Forte Agent 1.5/32.452 X-No-Archive: yes MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Mon, 23 Aug 1999 22:49:16 GMT NNTP-Posting-Host: 195.8.91.132 X-Complaints-To: abuse AT clara DOT net X-Trace: nnrp4.clara.net 935448556 195.8.91.132 (Mon, 23 Aug 1999 23:49:16 BST) NNTP-Posting-Date: Mon, 23 Aug 1999 23:49:16 BST Lines: 53 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com remove_this_mimo AT restoel DOT net_and_this (mimo) wrote: > i used to define my constants like this, following advice by someone > who seemed to understand the ansi/iso c++ declaration: > > class c{ > > const char cszText[] = "Text"; > const int ciNumber = 5; > > }; Those definitions would be ansi/iso outside of the class but ... > in version 2.95 of gcc this results in a compiler error. now i wonder > how am i supposed to initialize constants. i found out by trying that > the compiler accepts defining the consts values in the constructor, > like this: > > c::c() : cszText("Text"), ciNumber(5) {} That implies the reason you can't define the const's the way you want is because you would effectively be initialising them twice, which of course is not allowed. What did the error say? GCC 2.8.1 would allow many non-ansi things, it still does but the default behaviour has changed. With 2.8.1 the -pedantic switch is required (or -Wall which implies -pedantic) to get these warnings issued. With 2.9.5 the default appears to be -pedantic-errors, and -Wall no longer implies -pedantic. So now you get errors when before you maybe didn't even get a warning. If you use -pedantic with 2.9.5 the errors are downgraded to warnings so it should work just like 2.8.1. > class c{ > > // same as above > > char szBuffer[ciNumber]; > }; > > does not work any more... > > do i have to use #define's again like?! > or is there another way to cope with these things? Making ciNumber a global constant would be preferable to using #define, you may also want to investagate namespaces. If you are looking for an elegant solution to your problem or are interested in why the original code is not legal ansi (assuming that is the error) a post to comp.lang.c++ should be instructive. ;) -- george