Topic: Static member access


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Fri, 11 Feb 1994 11:41:06 GMT
Raw View
simon@sco.COM (Simon Tooke) writes:

>jason@cygnus.com (Jason Merrill) writes:
>
>>11.5 says, "A friend or a member function of a derived class can access a
>>protected static member of a base class."  However, neither Cfront nor xlC
>>accept this code.  Thoughts?
>>
>>class A {
>>protected:
>>  static int i;
>>};
>>
>>class B: public A { };
>>
>>class C: public B {
>>public:
>>  void g () { B b; b.i; }
>>};
>
>The "access" that is allowed refers to members of "self";

What makes you think this?

>i.e. member function
>C::g() can refer to A::i (the 'i' encapsulated by the instance that is used
>as "this" in C:g()), but not b.i, because b is _another_ instance; `protected'
>only grants access within an instance.

ARM 11.5 (as quoted above) states that static members can be accessed,
no if's or but's.  In contrast, it is quite explicit in saying
that *nonstatic* members can be accessed "only through a pointer to,
reference to, or object of the derived class".  This condition
applies only to nonstatic members.

>The fact that A::i is static (and thus is identical to b.i) is irrelevant.

No, it's not.  The ARM carefully distinguishes between static and
nonstatic members.

--
Fergus Henderson - fjh@munta.cs.mu.OZ.AU




Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Sat, 12 Feb 1994 15:32:59 GMT
Raw View
freja@mip3035.Berkeley.EDU (Etienne Frejaville) writes:

>jason@cygnus.com (Jason Merrill) writes:
>|> 11.5 says, "A friend or a member function of a derived class can access a
>|> protected static member of a base class."  However, neither Cfront nor xlC
>|> accept this code.  Thoughts?
[code omitted]
>By opposition to the next sentence of 11.5 : "A friend or a member
>function of a derived class can access a protected nonstatic member of
>its base classes only through a pointer to, reference to, or object of
>the derived class (or any class derived from that class).", you have to
>understand that the following code is legal :
>
> class C : public B {
> public :
> void g() { i=1;}
> friend void fr() { i=1;}
>};
>
>but not your code !

Hmm... I interpret that part of the ARM differently.
It seems that there are two sentences, the first of which deals
with the situation for static members, and the second of which
deals with non-static members.  For non-static members, there
are a bunch of restrictions ("only through ... the derived class"),
but for static members, there are NO restrictions.
I can't see how you can read those restrictions as applying to
static members.

--
Fergus Henderson - fjh@munta.cs.mu.OZ.AU




Author: satish@sun.mcs.clarkson.edu (Satish Thatte)
Date: Sat, 5 Feb 1994 15:33:23 GMT
Raw View


Author: satish@sun.mcs.clarkson.edu (Satish Thatte)
Date: Sun, 6 Feb 1994 17:38:53 GMT
Raw View


Author: freja@mip3035.Berkeley.EDU (Etienne Frejaville)
Date: 8 Feb 94 08:48:04 GMT
Raw View
In article <JASON.94Feb2115346@deneb.cygnus.com>, jason@cygnus.com (Jason Merrill) writes:
|> 11.5 says, "A friend or a member function of a derived class can access a
|> protected static member of a base class."  However, neither Cfront nor xlC
|> accept this code.  Thoughts?
|>
|> class A {
|> protected:
|>   static int i;
|> };
|>
|> class B: public A { };
|>
|> class C: public B {
|> public:
|>   void g () { B b; b.i; }
|> };


By opposition to the next sentence of 11.5 : "A friend or a member function of a derived class can access a
protected nonstatic member of its base classes only through a pointer to, reference to, or object of the
derived class (or any class derived from that class).", you have to understand that the following code is legal :

 class C : public B {
 public :
 void g() { i=1;}
 friend void fr() { i=1;}
};

but not your code !


--------Etienne FREJAVILLE---Bull S.A   -------------------------------------
 BSP/OSO/MDW/LPS/TOPAS                  e-mail: E.Frejaville@frcl.bull.fr
 Rue Jean-Jaures, F6/1D/17, BP 53       tel: (33-1) 30806548
 78340 Les Clayes-sous-Bois, France     Fax: (33-1) 30807950








Author: jason@cygnus.com (Jason Merrill)
Date: Wed, 9 Feb 1994 00:12:43 GMT
Raw View
>>>>> Etienne Frejaville <freja@mip3035.Berkeley.EDU> writes:

> By opposition to the next sentence of 11.5 : "A friend or a member
> function of a derived class can access a protected nonstatic member of
           ^^^^^^^^^
> its base classes only through a pointer to, reference to, or object of
> the derived class (or any class derived from that class)."

Does this sentence, however, apply to static members?  I think that the two
sentences are placed together to emphasize the contrast between them.

Jason




Author: jason@cygnus.com (Jason Merrill)
Date: Wed, 2 Feb 1994 19:53:46 GMT
Raw View
11.5 says, "A friend or a member function of a derived class can access a
protected static member of a base class."  However, neither Cfront nor xlC
accept this code.  Thoughts?

class A {
protected:
  static int i;
};

class B: public A { };

class C: public B {
public:
  void g () { B b; b.i; }
};




Author: gregoryr@muppet.bt.co.uk (Gregory Russell)
Date: 3 Feb 1994 11:38:02 GMT
Raw View
In article 94Feb2115346@deneb.cygnus.com, jason@cygnus.com (Jason Merrill) writes:
> 11.5 says, "A friend or a member function of a derived class can access a
> protected static member of a base class."  However, neither Cfront nor xlC
> accept this code.  Thoughts?
>
> class A {
> protected:
>   static int i;
> };
>
> class B: public A { };
>
> class C: public B {
> public:
>   void g () { B b; b.i; }
> };



Surely i cannot be referenced in this way since i is defined as a member of A. But this should work:

 class C: public B {
 public:
   void g () { i=5; }
 };

Awaiting confirmation/correction

Greg Russell






Author: pkt@lpi.liant.com (Scott Turner)
Date: Thu, 3 Feb 1994 15:10:47 GMT
Raw View
In article <JASON.94Feb2115346@deneb.cygnus.com>, jason@cygnus.com (Jason
Merrill) wrote:

> 11.5 says, "A friend or a member function of a derived class can access a
> protected static member of a base class."  However, neither Cfront nor xlC
> accept this code.  Thoughts?

Cfront does not fully implement this rule.  For example:

    class A {
    protected:
      static int i;
    };

    class B: public A { void g(); friend void h(); };

    void B::g() { B::i; }   // OK by cfront
    void h () { B::i; }     // error by cfront

For protected access to static members to be analogous to non-static,
compilers must be at least as permissive as this rule indicates.  Cfront
is wrong in its handling of my example.

Your example differs, by accessing A::i through an object.

    void C::g () { B b; b.i; }

In this case there's another rule which Cfront and xlC appear to be
invoking:

    A friend or a member function of a derived class can access a
    protected nonstatic member of one of its base classes only
    through a pointer to, reference to, or object of the derived
    class (or any class derived from that class).

It is plain enough that this rule does not apply to static members
such as A::i.

Cfront blatantly errs in its handling of my first example.  Your example
is different; the compilers (Cfront and xlC) do something reasonable,
but their behavior differs from the ARM.
--
Scott Turner
Liant Software Corp.
959 Concord St., Framingham, MA 01701  USA
(508)872-8700
pkt@lpi.liant.com