private virtual functions - accessibility legality

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

    private virtual functions - accessibility legality

    Hi,
    Private functions can be virtual in C++ ( unlike Java) ? What is the
    use of private virtual functions ? Below is the code -

    #include <iostream>

    class Foo {
    public:
    virtual void test() { std::cout << "Foo::test( )" << std::endl; }
    };

    class Bar : public Foo {
    private:
    void test() { std::cout << "Bar::test( )" << std::endl; }
    friend void friendToBar(Bar *);
    };

    void notFriendToBar (Foo *pFoo) {
    pFoo->test();
    }

    void friendToBar (Bar *pBar) {
    pBar->test();
    }

    int main(int argc, char *argv[]) {
    Bar b;
    friendToBar(&b) ;
    notFriendToBar( &b);
    }

    Function Bar::test() is private to Bar. but stil we can access it
    outside Bar in notFriendToBar function. Is it legal ? Is not private
    functions are meant to be either accessible to friend or to its class
    only ?
  • Kai-Uwe Bux

    #2
    Re: private virtual functions - accessibility legality

    Vijay Meena wrote:
    Hi,
    Private functions can be virtual in C++ ( unlike Java) ? What is the
    use of private virtual functions ? Below is the code -
    >
    #include <iostream>
    >
    class Foo {
    public:
    virtual void test() { std::cout << "Foo::test( )" << std::endl; }
    };
    >
    class Bar : public Foo {
    private:
    void test() { std::cout << "Bar::test( )" << std::endl; }
    friend void friendToBar(Bar *);
    };
    >
    void notFriendToBar (Foo *pFoo) {
    pFoo->test();
    }
    >
    void friendToBar (Bar *pBar) {
    pBar->test();
    }
    >
    int main(int argc, char *argv[]) {
    Bar b;
    friendToBar(&b) ;
    notFriendToBar( &b);
    }
    >
    Function Bar::test() is private to Bar. but stil we can access it
    outside Bar in notFriendToBar function. Is it legal ? Is not private
    functions are meant to be either accessible to friend or to its class
    only ?
    Your function is public in Foo and notFriendToBar( ) takes a Foo* as an
    argument. I see no reason, why accessibility should kick in.


    Best

    Kai-Uwe Bux

    Comment

    • Vijay Meena

      #3
      Re: private virtual functions - accessibility legality

      But notFriendToBar( ) ultimately executes Bar::test() by using
      polymorphism, which is a private function to Bar. Also, does C++
      allows private function to be virtual (Unlike Java) ? I got the query
      when I started learning Java recently.

      Thanks,

      Vijay

      Comment

      • Kai-Uwe Bux

        #4
        Re: private virtual functions - accessibility legality

        Vijay Meena wrote:
        But notFriendToBar( ) ultimately executes Bar::test() by using
        polymorphism, which is a private function to Bar.
        a) Please quote the context of your reply (and do not top-post). The reason
        is the news protocol which does _not_ ensure that the context of your
        posting is visible otherwise.

        To restore context, here is the code in question:

        class Foo {
        public:
        virtual void test() { std::cout << "Foo::test( )" << std::endl; }
        };

        class Bar : public Foo {
        private:
        void test() { std::cout << "Bar::test( )" << std::endl; }
        friend void friendToBar(Bar *);
        };

        void notFriendToBar (Foo *pFoo) {
        pFoo->test();
        }

        int main(int argc, char *argv[]) {
        Bar b;
        notFriendToBar( &b);
        }


        b) What is _ultimately_ executed is of no concern. Accessibility in C++ is a
        compile time concept that can be checked locally, i.e., when compiling
        notFriendToBar( ).

        Also, does C++ allows private function to be virtual (Unlike Java) ?
        I don't know about Java, but C++ does allow private functions to be virtual.
        It just so happens that test() is public in Foo() and notFriendToBar( )
        takes a Foo* as its argument.

        I got the query when I started learning Java recently.
        Huh? From whom? Why would anybody ask about C++ when teaching Java?


        Best

        Kai-Uwe Bux

        Comment

        • Vijay Meena

          #5
          Re: private virtual functions - accessibility legality

          On Oct 19, 6:48 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
          a) Please quote the context of your reply (and do not top-post). The reason
          is the news protocol which does _not_ ensure that the context of your
          posting is visible otherwise.
          Thanks, I will take care of it.
          I don't know about Java, but C++ does allow private functions to be virtual.
          It just so happens that test() is public in Foo() and notFriendToBar( )
          takes a Foo* as its argument.
          Thanks again for the clarification.
          I got the query when I started learning Java recently.
          >
          Huh? From whom? Why would anybody ask about C++ when teaching Java?
          >
          I am learning by self.

          Comment

          • Salt_Peter

            #6
            Re: private virtual functions - accessibility legality

            On Oct 18, 9:30 pm, Vijay Meena <vijay.me...@gm ail.comwrote:
            But notFriendToBar( ) ultimately executes Bar::test() by using
            polymorphism, which is a private function to Bar. Also, does C++
            allows private function to be virtual (Unlike Java) ? I got the query
            when I started learning Java recently.
            >
            Thanks,
            >
            Vijay
            accessibility of Bar::test() is not involved. Polymorphism offers a
            different interface based on accesibilty offered in base class. There
            are a number of Idioms that use private virtual member functions with
            public non-virtual accessors, namely Non-Virtual Function Idiom.

            #include <iostream>

            class Foo
            {
            virtual void test() const { std::cout << "Foo::test( )" <<
            std::endl; }
            public:
            void doit() const
            {
            test();
            }
            };

            class Bar : public Foo
            {
            void test() const { std::cout << "Bar::test( )" << std::endl; }
            friend void friendToBar(con st Bar&);
            };

            void notFriendToBar (const Foo& foo)
            {
            // foo.test(); // error
            foo.doit();
            }

            void friendToBar (const Bar& bar)
            {
            bar.test();
            }

            int main()
            {
            Bar b;
            friendToBar(b);
            notFriendToBar( b);
            }

            Comment

            • James Kanze

              #7
              Re: private virtual functions - accessibility legality

              On Oct 19, 2:34 am, Vijay Meena <vijay.me...@gm ail.comwrote:
              Private functions can be virtual in C++ ( unlike Java) ?  What
              is the use of private virtual functions ?
              The usual pattern is to define the contractual interface in
              public functions, then implement it in private functions and
              data. The contractual interface remains the same (or should
              remain the same) in all derived classes; you shouldn't be able
              to override it. So any virtual functions are usually private
              (although there are a number of exceptions---e.g. if there is an
              inversion of the call sequence, and the interface doesn't define
              any contract).
              Below is the code -
              #include <iostream>
              class Foo {
              public:
                      virtual void test() { std::cout << "Foo::test( )" << std::endl; }
              };
              class Bar : public Foo {
              private:
                      void test() { std::cout << "Bar::test( )" << std::endl; }
                      friend void friendToBar(Bar *);
              };
              void notFriendToBar (Foo *pFoo) {
                      pFoo->test();
              }
              void friendToBar (Bar *pBar) {
                      pBar->test();
              }
              int main(int argc, char *argv[]) {
                      Bar b;
                      friendToBar(&b) ;
                      notFriendToBar( &b);
              }
              Function Bar::test() is private to Bar. but stil we can access
              it outside Bar in notFriendToBar function. Is it legal ?
              It's legal. It's a consequence of the other rules; this
              particular use is not useful. (To date, I've found no useful
              reason for restricting the overriding function more than the
              function in the base class, but there's no real reason to ban it
              either. In the end, most virtual functions are private to begin
              with.)
              > Is not private functions are meant to be either accessible to
              friend or to its class only ?
              Yes, but there are ways of subverting this.

              --
              James Kanze (GABI Software) email:james.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              Working...