Topic: Changing access modifier of derived method


Author: Heinz Huber <Heinz.Huber@elbanet.co.at>
Date: 2000/01/26
Raw View

Christopher Eltschka wrote:
>
> Heinz Huber wrote:
> >
> > Martin von Loewis wrote:
> > >
> > > Huber Heinz <Heinz.Huber@elbanet.co.at> writes:
>
> [...]
>
> > > >       base = &priv1;
> > > >       base->test();   // which foo() is called?
> > >
> > > This calls Base::test, since test is not overridden in Priv1.
> >
> > What foo() is called?
>
> Since the dynamic type of the object is Priv1, and Priv1
> overrides foo(), Priv1::foo() is called.
>
> >
> > >
> > > >
> > > >     base = &priv2;
> > > >     base->test();   // which test() is called?
> > >
> > > This calls Priv2::test.
> >
> > Why can I call Priv2::test this way, although it is private?
>
> Because access check is done on the static type, that's Base here.
> The fact that test is overridden by a private method might
> not even be known at the calling site (the derived class could
> even not yet be written!).
> Indeed, the mere fact that Priv2::test is called is hidden
> from the caller. The caller just calls the virtual function
> test on a Base.

To sum both answers up, a base class has access to the virtual private
members of a derived class.

When calling a virtual function in the base class (or on a base class
pointer) that is declared private in a derived class, the derived
(private!) version is called anyway.

Correct?

Heinz

---
[ 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: Martin von Loewis <loewis@informatik.hu-berlin.de>
Date: 2000/01/22
Raw View
Huber Heinz <Heinz.Huber@elbanet.co.at> writes:

> Are you allowed to narrow / widen the access modifier of a function on
> derivation?

Yes, you can.

> Should the code below compile?

Yes, it should.

>  base = &priv1;
>  base->test();   // which foo() is called?

This calls Base::test, since test is not overridden in Priv1.

>
>     base = &priv2;
>     base->test();   // which test() is called?

This calls Priv2::test.


>  publ.foo();     // should be OK, I think

Indeed; Publ::foo is called.

Regards,
Martin

---
[ 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: Heinz Huber <Heinz.Huber@elbanet.co.at>
Date: 2000/01/24
Raw View


Martin von Loewis wrote:
>
> Huber Heinz <Heinz.Huber@elbanet.co.at> writes:
>
> > Are you allowed to narrow / widen the access modifier of a function on
> > derivation?
>
> Yes, you can.
>
> > Should the code below compile?
>
> Yes, it should.
>
> >       base = &priv1;
> >       base->test();   // which foo() is called?
>
> This calls Base::test, since test is not overridden in Priv1.

What foo() is called?

>
> >
> >     base = &priv2;
> >     base->test();   // which test() is called?
>
> This calls Priv2::test.

Why can I call Priv2::test this way, although it is private?

[snip]

Heinz


[ 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: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 2000/01/24
Raw View
Heinz Huber wrote:
>
> Martin von Loewis wrote:
> >
> > Huber Heinz <Heinz.Huber@elbanet.co.at> writes:

[...]

> > >       base = &priv1;
> > >       base->test();   // which foo() is called?
> >
> > This calls Base::test, since test is not overridden in Priv1.
>
> What foo() is called?

Since the dynamic type of the object is Priv1, and Priv1
overrides foo(), Priv1::foo() is called.

>
> >
> > >
> > >     base = &priv2;
> > >     base->test();   // which test() is called?
> >
> > This calls Priv2::test.
>
> Why can I call Priv2::test this way, although it is private?

Because access check is done on the static type, that's Base here.
The fact that test is overridden by a private method might
not even be known at the calling site (the derived class could
even not yet be written!).
Indeed, the mere fact that Priv2::test is called is hidden
from the caller. The caller just calls the virtual function
test on a Base.


[ 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: Huber Heinz <Heinz.Huber@elbanet.co.at>
Date: 2000/01/14
Raw View
Hi!

I've stumbled across an (at least for me) interesting question about
access modifiers and derivation.

Are you allowed to narrow / widen the access modifier of a function on
derivation?

Should the code below compile? What functions should be called?

Sample code:

class Base
{
 protected:
   virtual void foo();

 public:
   virtual void test();
};

void Base::test()
{
 foo();
}

class Priv1 : public Base
{
 private:
  virtual void foo();
};

class Priv2 : public Base
{
 private:
        virtual void test();
};

class Publ : public Base
{
 public:
  virtual void foo();
};


void test()
{
 Base *base;
 Priv1 priv1;
    Priv2 priv2;
 Publ publ;

 base = &priv1;
 base->test();   // which foo() is called?

    base = &priv2;
    base->test();   // which test() is called?

 publ.foo();     // should be OK, I think
}


Looking forward to answers,
Heinz


[ 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              ]