Topic: Subclass access to protected member via base class pointer


Author: Paul Black <paul.black@vf.vodafone.co.uk>
Date: 1998/06/14
Raw View
bobh@hablutzel.com (Bob Hablutzel) wrote:
> is line 2 legal or not? I maintain that it is, as the subclass is
> accessing a protected member of it's superclass. Some of my coworkers
> disagree. If it is not allowed, where does the standard (I have CD2)
> state
> this?

Section 11.5

Paul

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


[ 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: jim.hyslop@leitch.com
Date: 1998/06/14
Raw View
In article <6lm9cb$k71@netlab.cs.rpi.edu>,
  bobh@hablutzel.com (Bob Hablutzel) wrote:
>
> Some co-workers and I were having a debate about subclass access to
> protected members of their superclass, and we could not come to a final
> decision.
>
> The question has to do with subclass access to protected members of an
> object of their superclass, but not necessarily themselves. In other
> words, given:
>
> class Super
> {
> protected:
>
>    void ProtectedFunction( void );
> };
>
> class Sub : public Super
> {
>
> public:
>
>    void DoIt( Super* someSuper )
>    {
>       this->ProtectedFunction();          //1
>       someSuper->ProtectedFunction();     //2
>    }
> };
>
> Line 1 is clearly legal.
>
> is line 2 legal or not? I maintain that it is, as the subclass is
> accessing a protected member of it's superclass. Some of my coworkers
> disagree. If it is not allowed, where does the standard (I have CD2)
> state
> this?
Right in one of the examples:

Section 11.5, Protected Member Access:
1 ... Except when forming a pointer to member (5.3.1), the access must be
through a pointer to, reference to, or object of the derived class itself (or
any class derived from that class).

[Example:
(Elided somewhat to show only relevant portions of the example - Jim)
class B {
protected:
  int i;
};

class D1 : public B {...};

class D2 : public B {
  void mem(B*,D1*);
};

void D2::mem(B* pb, D1* p1)
{
  pb   >i = 1; // ill   formed
  p1   >i = 2; // ill   formed
  i = 3; // ok (access through    this   )
  B::i = 4; // ok (access through    this   , qualification ignored)
}

--- end quote ---

I got tripped up on this myself recently.

Jim

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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: bobh@hablutzel.com (Bob Hablutzel)
Date: 1998/06/10
Raw View
Some co-workers and I were having a debate about subclass access to
protected members of their superclass, and we could not come to a final
decision.

The question has to do with subclass access to protected members of an
object of their superclass, but not necessarily themselves. In other
words, given:

class Super
{
protected:

   void ProtectedFunction( void );
};


class Sub : public Super
{

public:

   void DoIt( Super* someSuper )
   {
      this->ProtectedFunction();          //1
      someSuper->ProtectedFunction();     //2
   }
};

Line 1 is clearly legal.

is line 2 legal or not? I maintain that it is, as the subclass is
accessing a protected member of it's superclass. Some of my coworkers
disagree. If it is not allowed, where does the standard (I have CD2)
state
this?

  {Line 1 is legal. Line 2 is not. Think of it this way: You don't
  get extra "rights" to base objects just because your class
  is derived from their class. -mod}

VC++ does not allow line 1, nor does IBM xlC. gcc does.

--
Bob Hablutzel                           Hablutzel Consulting
bobh@hablutzel.com                      (603) 431-5074


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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              ]