Private virt function can be called with Base class ptr

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vijay

    Private virt function can be called with Base class ptr

    Hello
    I wanted to understand a contradictory design of C++

    class A
    {public:
    virtual void f(){ cout<<" base f"<<endl; }
    };

    class B:public A
    {
    private:
    void f(){ cout<<"private derived B f"<<endl;};
    };
    main()
    {
    B b;
    A* p = &b;
    p->f();
    }

    why does the compiler allows me to call a private function in B though
    accessing it directly is not possible;(B b; b.f() gives error)


    Vijay


  • Alf P. Steinbach

    #2
    Re: Private virt function can be called with Base class ptr

    On Fri, 22 Aug 2003 11:49:27 +0530, "vijay" <getvijay2k@yah oo.com> wrote:
    [color=blue]
    >Hello
    >I wanted to understand a contradictory design of C++
    >
    >class A
    >{public:
    > virtual void f(){ cout<<" base f"<<endl; }
    >};
    >
    >class B:public A
    >{
    >private:
    > void f(){ cout<<"private derived B f"<<endl;};
    > };
    >main()[/color]

    Just a note: the standard requires 'main' to have return type 'int'.

    [color=blue]
    >{
    > B b;
    > A* p = &b;
    > p->f();
    >}
    >
    >why does the compiler allows me to call a private function in B though
    >accessing it directly is not possible;(B b; b.f() gives error)[/color]

    That's no big mystery. The compiler uses the statically available type,
    namely A. It cannot check at compile time the dynamic type of *p (well,
    it can in this particular case, but not in general).

    More difficult: why does C++ allow you to change a method from 'public'
    to 'private' in a derived class?

    Well, it can be useful in some cases. Determining in which cases it's
    more confusing than useful would be hard, if not practically impossible.
    And so C++ allows it, because 'the programmer knows best'; some other
    languages disallow it, because 'the compiler knows best'.

    Comment

    • Attila Feher

      #3
      Re: Private virt function can be called with Base class ptr

      vijay wrote:[color=blue]
      > Hello
      > I wanted to understand a contradictory design of C++
      >
      > class A
      > {public:
      > virtual void f(){ cout<<" base f"<<endl; }
      > };
      >
      > class B:public A
      > {
      > private:
      > void f(){ cout<<"private derived B f"<<endl;};
      > };
      > main()
      > {
      > B b;
      > A* p = &b;
      > p->f();
      > }
      >
      > why does the compiler allows me to call a private function in B though
      > accessing it directly is not possible;(B b; b.f() gives error)[/color]

      Actually - I should say - it is your design, which is flawed, not C++. The
      function f() is part of your public interface. C++ allows you to put it
      private in the derived class (it even allows you to put it private in a base
      class and then later override it and bring it public) and make it public in
      the base, but that usually makes absolutely no sense to do so. In most
      cases classes in a hierarchy must conform to the Liskov Substitution
      Principle ( http://tinyurl.com/ktb5 [

      _principle.htm ] )
      which in turn requires all derived classes to be substitutable in place of
      the base. Shortly: whereever you can use a type A object, you must be able
      to use a type B object as well. Now this - in turn - means that you publish
      your interface in class A, and you will not change it or its meaning in any
      subsequent derivations.

      The wuestion (why does C++ allow it) is valid, but IMHO not so disturbing.
      As Alf described it very well, C++ takes the route of not limiting the
      programmers actions rather than trying to figure out what is good or wrong.
      Now I do not recall any use for this scenario you have just shown to us -
      but it may very well be my memory, it does not mean that there is no use for
      this.

      C++ has been designed at the beginning of OO. What I mean is that at that
      time OO was not in widespread use, C++ was new as well etc. so the designers
      of the language decided that unless they can prove that some combination of
      features is absolutely useless or definitely harmful, they will allow it. I
      guess this is one example of this thinking here.

      Have you asked about the same "trick" the other way around, I could have
      talked about the idiomatic C++ implementation of the Template Method (
      http://c2.com/cgi/wiki?TemplateMethodPattern )pattern. In the template
      method implementation in C++ the virtual hooks for the non-virtual template
      methods are defined as private in the base class. This is done so, because
      they must not be used directly, only indirectly using the template method.
      In the subclasses it is not possible to call them, but it is possible to
      override them - this overriding is also done as private functions. So while
      in that pattern we do not change between public/private, a similarly
      astonishing trick is used: we cannot call base class private virtual
      function, but we can "rewrite" them in a subclass. (Of course if those
      hooks might be useful in a subclass they will be declared as protected, but
      then there is no trick. :-) )


      --
      Attila aka WW


      Comment

      • Param

        #4
        Re: Private virt function can be called with Base class ptr

        Hi
        The simple principle followed by C++ in accessing virtual functions is
        as follows...
        1. Availability .
        Whether the function u are calling is available... In this case it is
        available in the class A
        2. Accessibility :
        Whether the function u are calling is accessible .. It is accessible
        in Class A which is public
        3. Virtiual :
        Whether the function is virtual . . It is in this case... The compiler
        will built a vtable and store the address of function of the derived class
        function.
        As far as the access check is concerned it is done at compile time only ..
        So it passes through and u can bypass the private access of the derived
        class. The pointer u are using is still the base class pointer.
        If u try the reverse way i mean u make base class function private and
        derived class function public the compilation wont go through because the
        compiler will fail in the second step..

        So this is the reason why it is possible.

        Param
        Siemens Information Systems Ltd


        "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
        news:bi4f2b$rg0 $1@newstree.wis e.edt.ericsson. se...[color=blue]
        > vijay wrote:[color=green]
        > > Hello
        > > I wanted to understand a contradictory design of C++
        > >
        > > class A
        > > {public:
        > > virtual void f(){ cout<<" base f"<<endl; }
        > > };
        > >
        > > class B:public A
        > > {
        > > private:
        > > void f(){ cout<<"private derived B f"<<endl;};
        > > };
        > > main()
        > > {
        > > B b;
        > > A* p = &b;
        > > p->f();
        > > }
        > >
        > > why does the compiler allows me to call a private function in B though
        > > accessing it directly is not possible;(B b; b.f() gives error)[/color]
        >
        > Actually - I should say - it is your design, which is flawed, not C++.[/color]
        The[color=blue]
        > function f() is part of your public interface. C++ allows you to put it
        > private in the derived class (it even allows you to put it private in a[/color]
        base[color=blue]
        > class and then later override it and bring it public) and make it public[/color]
        in[color=blue]
        > the base, but that usually makes absolutely no sense to do so. In most
        > cases classes in a hierarchy must conform to the Liskov Substitution
        > Principle ( http://tinyurl.com/ktb5 [
        >[/color]
        http://www.eventhelix.com/RealtimeMa...v_substitution[color=blue]
        > _principle.htm ] )
        > which in turn requires all derived classes to be substitutable in place of
        > the base. Shortly: whereever you can use a type A object, you must be[/color]
        able[color=blue]
        > to use a type B object as well. Now this - in turn - means that you[/color]
        publish[color=blue]
        > your interface in class A, and you will not change it or its meaning in[/color]
        any[color=blue]
        > subsequent derivations.
        >
        > The wuestion (why does C++ allow it) is valid, but IMHO not so disturbing.
        > As Alf described it very well, C++ takes the route of not limiting the
        > programmers actions rather than trying to figure out what is good or[/color]
        wrong.[color=blue]
        > Now I do not recall any use for this scenario you have just shown to us -
        > but it may very well be my memory, it does not mean that there is no use[/color]
        for[color=blue]
        > this.
        >
        > C++ has been designed at the beginning of OO. What I mean is that at that
        > time OO was not in widespread use, C++ was new as well etc. so the[/color]
        designers[color=blue]
        > of the language decided that unless they can prove that some combination[/color]
        of[color=blue]
        > features is absolutely useless or definitely harmful, they will allow it.[/color]
        I[color=blue]
        > guess this is one example of this thinking here.
        >
        > Have you asked about the same "trick" the other way around, I could have
        > talked about the idiomatic C++ implementation of the Template Method (
        > http://c2.com/cgi/wiki?TemplateMethodPattern )pattern. In the template
        > method implementation in C++ the virtual hooks for the non-virtual[/color]
        template[color=blue]
        > methods are defined as private in the base class. This is done so,[/color]
        because[color=blue]
        > they must not be used directly, only indirectly using the template method.
        > In the subclasses it is not possible to call them, but it is possible to
        > override them - this overriding is also done as private functions. So[/color]
        while[color=blue]
        > in that pattern we do not change between public/private, a similarly
        > astonishing trick is used: we cannot call base class private virtual
        > function, but we can "rewrite" them in a subclass. (Of course if those
        > hooks might be useful in a subclass they will be declared as protected,[/color]
        but[color=blue]
        > then there is no trick. :-) )
        >
        >
        > --
        > Attila aka WW
        >
        >[/color]


        Comment

        • Attila Feher

          #5
          Re: Private virt function can be called with Base class ptr

          Param wrote:[color=blue]
          > Hi
          > The simple principle followed by C++ in accessing virtual
          > functions is as follows...[/color]
          [SNIP]

          a.) Why do you post this as a reply to me?

          b.) Why do you top-post?


          BTW the OP did not ask for what - mechanism make this work in this way -
          but - why is this nonsense allowed (if you let me exaggerate). At least
          that was my understanding.

          --
          Attila aka WW




          Comment

          • vijay

            #6
            Re: Private virt function can be called with Base class ptr

            Thanks param,But I know this,
            u can look at my question that why does c++ design allowed this !!!!
            vijay

            "Param" <secparam@yahoo .com> wrote in message
            news:bi4qd3$4i2 $1@news.mch.sbs .de...[color=blue]
            > Hi
            > The simple principle followed by C++ in accessing virtual functions is
            > as follows...
            > 1. Availability .
            > Whether the function u are calling is available... In this case it[/color]
            is[color=blue]
            > available in the class A
            > 2. Accessibility :
            > Whether the function u are calling is accessible .. It is[/color]
            accessible[color=blue]
            > in Class A which is public
            > 3. Virtiual :
            > Whether the function is virtual . . It is in this case... The[/color]
            compiler[color=blue]
            > will built a vtable and store the address of function of the derived class
            > function.
            > As far as the access check is concerned it is done at compile time only ..
            > So it passes through and u can bypass the private access of the derived
            > class. The pointer u are using is still the base class pointer.
            > If u try the reverse way i mean u make base class function private and
            > derived class function public the compilation wont go through because the
            > compiler will fail in the second step..
            >
            > So this is the reason why it is possible.
            >
            > Param
            > Siemens Information Systems Ltd
            >
            >
            > "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
            > news:bi4f2b$rg0 $1@newstree.wis e.edt.ericsson. se...[color=green]
            > > vijay wrote:[color=darkred]
            > > > Hello
            > > > I wanted to understand a contradictory design of C++
            > > >
            > > > class A
            > > > {public:
            > > > virtual void f(){ cout<<" base f"<<endl; }
            > > > };
            > > >
            > > > class B:public A
            > > > {
            > > > private:
            > > > void f(){ cout<<"private derived B f"<<endl;};
            > > > };
            > > > main()
            > > > {
            > > > B b;
            > > > A* p = &b;
            > > > p->f();
            > > > }
            > > >
            > > > why does the compiler allows me to call a private function in B though
            > > > accessing it directly is not possible;(B b; b.f() gives error)[/color]
            > >
            > > Actually - I should say - it is your design, which is flawed, not C++.[/color]
            > The[color=green]
            > > function f() is part of your public interface. C++ allows you to put it
            > > private in the derived class (it even allows you to put it private in a[/color]
            > base[color=green]
            > > class and then later override it and bring it public) and make it public[/color]
            > in[color=green]
            > > the base, but that usually makes absolutely no sense to do so. In most
            > > cases classes in a hierarchy must conform to the Liskov Substitution
            > > Principle ( http://tinyurl.com/ktb5 [
            > >[/color]
            >[/color]
            http://www.eventhelix.com/RealtimeMa...v_substitution[color=blue][color=green]
            > > _principle.htm ] )
            > > which in turn requires all derived classes to be substitutable in place[/color][/color]
            of[color=blue][color=green]
            > > the base. Shortly: whereever you can use a type A object, you must be[/color]
            > able[color=green]
            > > to use a type B object as well. Now this - in turn - means that you[/color]
            > publish[color=green]
            > > your interface in class A, and you will not change it or its meaning in[/color]
            > any[color=green]
            > > subsequent derivations.
            > >
            > > The wuestion (why does C++ allow it) is valid, but IMHO not so[/color][/color]
            disturbing.[color=blue][color=green]
            > > As Alf described it very well, C++ takes the route of not limiting the
            > > programmers actions rather than trying to figure out what is good or[/color]
            > wrong.[color=green]
            > > Now I do not recall any use for this scenario you have just shown to[/color][/color]
            us -[color=blue][color=green]
            > > but it may very well be my memory, it does not mean that there is no use[/color]
            > for[color=green]
            > > this.
            > >
            > > C++ has been designed at the beginning of OO. What I mean is that at[/color][/color]
            that[color=blue][color=green]
            > > time OO was not in widespread use, C++ was new as well etc. so the[/color]
            > designers[color=green]
            > > of the language decided that unless they can prove that some combination[/color]
            > of[color=green]
            > > features is absolutely useless or definitely harmful, they will allow[/color][/color]
            it.[color=blue]
            > I[color=green]
            > > guess this is one example of this thinking here.
            > >
            > > Have you asked about the same "trick" the other way around, I could have
            > > talked about the idiomatic C++ implementation of the Template Method (
            > > http://c2.com/cgi/wiki?TemplateMethodPattern )pattern. In the template
            > > method implementation in C++ the virtual hooks for the non-virtual[/color]
            > template[color=green]
            > > methods are defined as private in the base class. This is done so,[/color]
            > because[color=green]
            > > they must not be used directly, only indirectly using the template[/color][/color]
            method.[color=blue][color=green]
            > > In the subclasses it is not possible to call them, but it is possible to
            > > override them - this overriding is also done as private functions. So[/color]
            > while[color=green]
            > > in that pattern we do not change between public/private, a similarly
            > > astonishing trick is used: we cannot call base class private virtual
            > > function, but we can "rewrite" them in a subclass. (Of course if those
            > > hooks might be useful in a subclass they will be declared as protected,[/color]
            > but[color=green]
            > > then there is no trick. :-) )
            > >
            > >
            > > --
            > > Attila aka WW
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Shane Beasley

              #7
              Re: Private virt function can be called with Base class ptr

              "vijay" <getvijay2k@yah oo.com> wrote in message news:<bi4c7q$73 g$1@news.mch.sb s.de>...
              [color=blue]
              > class A
              > {public:
              > virtual void f(){ cout<<" base f"<<endl; }
              > };
              >
              > class B:public A
              > {
              > private:
              > void f(){ cout<<"private derived B f"<<endl;};
              > };
              > main()
              > {
              > B b;
              > A* p = &b;
              > p->f();
              > }
              >
              > why does the compiler allows me to call a private function in B though
              > accessing it directly is not possible;(B b; b.f() gives error)[/color]

              All the compiler is allowed to consider when compiling p->f() is that
              p is a pointer to A and A::f is public. Access specifiers are a
              compile-time feature and are not checked when dispatching virtual
              function calls at run-time.

              More importantly: Never attempt to hide a function inherited from a
              public base class. Users can always access any public function in a
              public base class through a pointer or reference to that class, even
              if it refers to a derived class where that function is hidden. If you
              can't support the entirety of the base class interface, don't inherit
              it -- at least, not publicly:

              class Base {
              public: void f ();
              void g ();
              };

              // private inheritance and using declarations
              class PrivateInherita nce : private Base {
              public: using Base::f; // make Base::f accessible
              };

              // composition and proxy methods
              class Composition {
              public: void f () { myBase.f(); } // make Base::f accessible
              private: Base myBase;
              };

              See also this thread:

              <http://groups.google.c om/groups?th=9aacc 58aeaa1bc4>

              - Shane

              Comment

              Working...