Topic: Q: const int member initialization in a class
Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/08/13 Raw View
"Jim Cobban" <Jim.Cobban.jcobban@nt.com> writes:
>In article <slrn6t3lc8.hn3.sbnaran1@bardeen.ceg.uiuc.edu>,
>Siemel Naran <sbnaran@KILL.uiuc.edu> wrote:
>>On 12 Aug 1998 16:58:41 GMT, Abhay Kanhere <akanhere@acm.org> wrote:
>>
>>> class myclass {
>>> public:
>>> const int MAX_SIZE=32;
>>
>>It should be "static const" or an "enum".
>As pointed out on another thread you cannot initialize a static const member
>of a class in the class declaration.
You can, according to the standard, section 9.4.2, "Static data members":
"If a static data member is of const integral or const enumeration type,
its declaration in the class definition can specify a constant-
initializer which shall be an integral constant expression (5.19).
In that case, the member can appear in integral constant expressions
within its scope. The member shall still be defined in a namespace
scope if it is used in the program and the namespace scope definition
shall not contain an initializer."
--
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Abhay Kanhere <akanhere@acm.org>
Date: 1998/08/12 Raw View
Hello,
Is this valid under the latest draft std ?
gcc 2.8.1 flags ANSI C++ violation (with option
-pedantic-errors) on this.
<code>
class myclass {
public:
const int MAX_SIZE=32;
//<-------violation ?
private:
int my_array[MAX_SIZE];
// don't want heaped alloc;
public:
myclass(){};
};
</code>
If this form is not allowed, how is one expected to
provide a value of const MAX_SIZE without cluttering
the global namespace or using C style macro ?
- Abhay
-----------------------------------------
mailto: akanhere@acm.org
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1998/08/12 Raw View
On 12 Aug 1998 16:58:41 GMT, Abhay Kanhere <akanhere@acm.org> wrote:
> class myclass {
> public:
> const int MAX_SIZE=32;
It should be "static const" or an "enum".
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1998/08/12 Raw View
On 12 Aug 1998 16:58:41 GMT, Abhay Kanhere <akanhere@acm.org> wrote:
> class myclass {
> public:
> const int MAX_SIZE=32;
It should be "static const" or an "enum".
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern/std-c++/faq.html ]
Author: "Jim Cobban" <Jim.Cobban.jcobban@nt.com>
Date: 1998/08/13 Raw View
In article <slrn6t3lc8.hn3.sbnaran1@bardeen.ceg.uiuc.edu>,
Siemel Naran <sbnaran@KILL.uiuc.edu> wrote:
>On 12 Aug 1998 16:58:41 GMT, Abhay Kanhere <akanhere@acm.org> wrote:
>
>> class myclass {
>> public:
>> const int MAX_SIZE=32;
>
>It should be "static const" or an "enum".
As pointed out on another thread you cannot initialize a static const member
of a class in the class declaration. This is probably an oversight since
it is inconsistent with the treatment of static const objects in global
scope. Therefore you are left with an enumeration:
enum {MAX_SIZE=32}:
--
Jim Cobban | jcobban@nortel.ca | Phone: (613) 763-8013
Nortel (MCS) | | FAX: (613) 763-5199
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]