Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initializing static member data

2 views
Skip to first unread message

Cliff Bryant, Jr.

unread,
Feb 6, 1998, 3:00:00 AM2/6/98
to

I am taking a course in data structures at Boston University. The book that
we are using, Main, M. & Savitch, W., "Data Structures and Other Objects
Using C++," claims that you can initialize static member variables in the
class, viz.,

class foo {
static const int bar = 10;
};

When I learned about static member variables, I learned to do the following,

// In header file
class foo2 {
static const int bar2;
};

// In implementation file
const int foo2::bar2 = 10;

I have looked at Bjarne Stroustrup's 3rd edition of "The C++ Programming
Language," and the C++ FAQ Lite web page, and I can find no mention of the
author's construction. They claim that it was part of the February 1995
Draft of the C++ Standard. Can anyone shed any light on this issue?

Clifford M. Bryant, Jr.
Senior Software Engineer
Atex Media Solutions, Inc.
cbr...@atex.com

Jason Smith

unread,
Feb 6, 1998, 3:00:00 AM2/6/98
to

Cliff Bryant, Jr. wrote in message ...
=Main, M. & Savitch, W., "Data Structures and Other Objects
=Using C++," claims that you can initialize static member variables in the
=class, viz.,
=
=class foo {
= static const int bar = 10;
=};
=
=When I learned about static member variables, I learned to do the
following,
=
=// In header file
=class foo2 {
= static const int bar2;
=};
=
=// In implementation file
=const int foo2::bar2 = 10;

The const keyword makes the above code invalid (according to the pre-ISO de
facto standard of the ARM).

The construct the author's speak of is specifically for *const* integral
(signed/unsigned char, short, int, long or const enumeration) static
members, and was indeed added to the ISO standard (section 9.4.2, paragraph
4):

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
(_expr.const_). In that case, the member can appear in integral con-
stant 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.

Jason.

0 new messages