Topic: Scope of static initializer block
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/06/25 Raw View
I (David R Tribble <david.tribble@central.beasys.com>) wrote on 5/29/97:
> The Draft Standard defines several kinds of 'scope', i.e., the windows in
> which a name is visible, in section 3.3. However, it does not mention a
> separate scope within the braces of a static initializer.
>
> Consider this example, in which I declare a class with two static array
> members, one public and the other private:
>
> class Foo
> {
> public:
> static Bar pubtab[]; // Public static member array
> private:
> static Baz privtab[]; // Private static member array
> };
>
> I'd like to define Foo::pubtab[] with a static initializer, which should be
> okay as long as class Bar is a POD type. So, I declare class Bar as a POD,
> with a member that points to a Baz:
>
> class Bar // a POD class
> {
> public:
> const Baz * ptr;
> ...
> };
>
> Thus I should be able to code my initializer for Foo::pubtab like this:
>
> Bar Foo::pubtab[] = // Initializer for array of PODs
> {
> { &privtab[0], ... }, // privtab or Foo::privtab?
> { &privtab[1], ... },
> ...
> };
>
> My question is, do I need to qualify the reference to privtab in the
> initializers (e.g., &Foo::privtab[0], etc.), or does the inside of the
> initializer constitute a scope, ala function scope? In other words, are
> expressions inside an initializer block limited to global scope? Does the
> initializer for a member of class Foo know about (have access to) the other
> members of class Foo? Sections 3.3 and 3.4.5 in the draft apply, but don't
> appear to answer the question.
>
> (Another thought occurs to me. Since Bar::ptr is public, yet points to a
> private class member (&Foo::privtab[i]), this opens a hole in the access
> safety net. Of course, this is true of any public class member pointer
> variable that points to any private data, so I don't think this is relevant;
> my question is about scope, not protection.)
Well, I finally found the answer. Initializers for member variables have
the same scope as member functions, following the '='. Thus, initializers
do not need to qualify class member names within initializer expressions.
See: 3.4.1 [basic.lookup.unqual], 9.4.2 [class.static.data], and 3.3.6
[basic.scope.class].
(I would like to say thanks to all those who helped in answering my question,
but nobody did, which is unusual.)
--------------------. BEA Systems, Inc. ,-. +1-972-738-6125 Office
David R. Tribble \ ,------------------' \ +1-972-738-6111 Fax
http://www.beasys.com `-' Dallas, TX 75248 `-----------------------
david.tribble@noSPAM.beasys.com http://www.flash.net/~dtribble
-------------------------------------------------------------------------
Send junk to: r_denterprise@hotmail.com teamwork@1stfamily.com
lvck1@hotmail.com bubbac@webconnect.com clover@earthfriends.com
abuse@cyberpromo.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/05/29 Raw View
The Draft Standard defines several kinds of 'scope', i.e., the windows in
which a name is visible, in section 3.3. However, it does not mention a
separate scope within the braces of a static initializer.
Consider this example, in which I declare a class with two static array
members, one public and the other private:
class Foo
{
public:
static Bar pubtab[]; // Public static member array
private:
static Baz privtab[]; // Private static member array
};
I'd like to define Foo::pubtab[] with a static initializer, which should be
okay as long as class Bar is a POD type. So, I declare class Bar as a POD,
with a member that points to a Baz:
class Bar // a POD class
{
public:
const Baz * ptr;
...
};
Thus I should be able to code my initializer for Foo::pubtab like this:
Bar Foo::pubtab[] = // Initializer for array of PODs
{
{ &privtab[0], ... }, // privtab or Foo::privtab?
{ &privtab[1], ... },
...
};
My question is, do I need to qualify the reference to privtab in the
initializers (e.g., &Foo::privtab[0], etc.), or does the inside of the
initializer constitute a scope, ala function scope? In other words, are
expressions inside an initializer block limited to global scope? Does the
initializer for a member of class Foo know about (have access to) the other
members of class Foo? Sections 3.3 and 3.4.5 in the draft apply, but don't
appear to answer the question.
(Another thought occurs to me. Since Bar::ptr is public, yet points to a
private class member (&Foo::privtab[i]), this opens a hole in the access
safety net. Of course, this is true of any public class member pointer
variable that points to any private data, so I don't think this is relevant;
my question is about scope, not protection.)
--------------------. BEA Systems, Inc. ,-. +1-972-738-6125 Office
David R. Tribble \ ,------------------' \ +1-972-738-6111 Fax
http://www.beasys.com `-' Dallas, TX 75248 `-----------------------
noSPAM.david.tribble@central.beasys.com http://www.flash.net/~dtribble
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]