Changing access specifier for virtual function

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

    Changing access specifier for virtual function

    Consider the following code

    #include <iostream>

    class Base {
    public:
    virtual void say() { std::cout << "Base" << std::endl; }
    };

    class Derived: public base {
    private:
    void say() { std::cout << "Derived" << std::endl; }
    };

    int main() {
    Derived d;
    Base* b = &d;
    b->say();
    return 0;
    }

    Running this program produces following output.

    $ ./a.out
    Derived
    $

    I have 2 questions:
    1. Is the above code legal ?
    2. If the virtual function is private in Base, can I make it public in
    Derived ?

    Thanks

  • Thomas Tutone

    #2
    Re: Changing access specifier for virtual function

    dragoncoder wrote:[color=blue]
    > Consider the following code
    >
    > #include <iostream>
    >
    > class Base {
    > public:
    > virtual void say() { std::cout << "Base" << std::endl; }
    > };
    >
    > class Derived: public base {
    > private:
    > void say() { std::cout << "Derived" << std::endl; }
    > };
    >
    > int main() {
    > Derived d;
    > Base* b = &d;
    > b->say();
    > return 0;
    > }
    >
    > Running this program produces following output.
    >
    > $ ./a.out
    > Derived
    > $
    >
    > I have 2 questions:
    > 1. Is the above code legal ?[/color]

    Yes. And an excellent example of poor coding practice.
    [color=blue]
    > 2. If the virtual function is private in Base, can I make it public in
    > Derived ?[/color]

    No. If it were private in Base, then Derived wouldn't inherit it.

    Best regards,

    Tom

    Comment

    • Victor Bazarov

      #3
      Re: Changing access specifier for virtual function

      dragoncoder wrote:[color=blue]
      > Consider the following code
      >
      > #include <iostream>
      >
      > class Base {
      > public:
      > virtual void say() { std::cout << "Base" << std::endl; }
      > };
      >
      > class Derived: public base {[/color]

      You mean

      class Derived: public Base {
      [color=blue]
      > private:
      > void say() { std::cout << "Derived" << std::endl; }
      > };
      >
      > int main() {
      > Derived d;
      > Base* b = &d;
      > b->say();
      > return 0;
      > }
      >
      > Running this program produces following output.
      >
      > $ ./a.out
      > Derived
      > $
      >
      > I have 2 questions:
      > 1. Is the above code legal ?[/color]

      Yes, absolutely, considering the correction I made. Try not to type your
      code directly into the message next time. Use "copy-and-paste" mechanism
      undoubtedly available to you.
      [color=blue]
      > 2. If the virtual function is private in Base, can I make it public in
      > Derived ?[/color]

      The access specifier only has effect on the ability to call the function
      as if it weren't virtual. For example, if you make 'say' private in 'Base',
      your call to it in 'main' (b->say()) will be ill-formed, and won't compile.

      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask


      Comment

      • Victor Bazarov

        #4
        Re: Changing access specifier for virtual function

        Thomas Tutone wrote:[color=blue]
        > dragoncoder wrote:[color=green]
        >> Consider the following code
        >>
        >> #include <iostream>
        >>
        >> class Base {
        >> public:
        >> virtual void say() { std::cout << "Base" << std::endl; }
        >> };
        >>
        >> class Derived: public base {
        >> private:
        >> void say() { std::cout << "Derived" << std::endl; }
        >> };
        >>
        >> int main() {
        >> Derived d;
        >> Base* b = &d;
        >> b->say();
        >> return 0;
        >> }
        >>
        >> Running this program produces following output.
        >>
        >> $ ./a.out
        >> Derived
        >> $
        >>
        >> I have 2 questions:
        >> 1. Is the above code legal ?[/color]
        >
        > Yes. And an excellent example of poor coding practice.[/color]

        Why do you call this practice "poor"?
        [color=blue][color=green]
        >> 2. If the virtual function is private in Base, can I make it public
        >> in Derived ?[/color]
        >
        > No. If it were private in Base, then Derived wouldn't inherit it.[/color]

        Nothing is inherited here. It's _overridden_. Try it:

        #include <iostream>

        class Base {
        virtual int foo() { return 42; } // private
        public:
        int bar() { return foo(); }
        };

        class Derived : public Base {
        virtual int foo() { return 73; } // also private
        };

        int main()
        {
        Derived d;
        Base *b = &d;
        std::cout << b->bar() << std::endl;
        return 0;
        }

        What happens here?

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        • Tejas Kokje

          #5
          Re: Changing access specifier for virtual function

          dragoncoder wrote:[color=blue]
          > Consider the following code
          >
          > #include <iostream>
          >
          > class Base {
          > public:
          > virtual void say() { std::cout << "Base" << std::endl; }
          > };
          >
          > class Derived: public base {
          > private:
          > void say() { std::cout << "Derived" << std::endl; }
          > };
          >
          > int main() {
          > Derived d;
          > Base* b = &d;
          > b->say();
          > return 0;
          > }
          >
          > Running this program produces following output.
          >
          > $ ./a.out
          > Derived
          > $
          >
          > I have 2 questions:
          > 1. Is the above code legal ?[/color]

          Since the above code compiled, it is syntactically legal.
          [color=blue]
          > 2. If the virtual function is private in Base, can I make it public in
          > Derived ?[/color]

          The call to b->say() is calling Derived class say() function. Here
          Derived class does not know anything about say() function of Base class
          since say() is private in Base(). Hence say() functions in Derived and
          Base class are not related by virtual function paradigm.

          Even if you remove "virtual" specifier from Base class say() function,
          you will still run Derived class say() function since say() is public
          in Derived and there is no public say() in Base class.

          Tejas Kokje

          Comment

          • Phlip

            #6
            Re: Changing access specifier for virtual function

            Tejas Kokje wrote:
            [color=blue][color=green]
            >> 1. Is the above code legal ?[/color]
            >
            > Since the above code compiled, it is syntactically legal.[/color]

            Famous last words.

            Try: "The code is well-formed, so it should compile."

            Plenty of compilers reject plenty of well-formed constructions.

            Yes, the OP is correct to rely on their compiler to judge how well such a
            simple bit of code is formed!

            --
            Phlip
            http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

            Comment

            • Tejas Kokje

              #7
              Re: Changing access specifier for virtual function

              Phlip wrote:[color=blue]
              > Tejas Kokje wrote:
              >[color=green][color=darkred]
              > >> 1. Is the above code legal ?[/color]
              > >
              > > Since the above code compiled, it is syntactically legal.[/color]
              >
              > Famous last words.
              >
              > Try: "The code is well-formed, so it should compile."[/color]
              [color=blue]
              > Plenty of compilers reject plenty of well-formed constructions.
              > Yes, the OP is correct to rely on their compiler to judge how well such a
              > simple bit of code is formed![/color]

              I think you misunderstood my comments. I said that the code is
              *syntactically* legal. This does not mean that code semantics make
              sense. If compilers could detect semantic errors in code, most of the
              softwares would be bug free.

              Anyways, in OP's case, compiler did compile the code. So there is no
              reason for him/her to see the code as illegal. However, OP is not
              relying on compiler to tell him that code is well formed. Hence he/she
              posted a question here.

              Tejas Kokje

              Comment

              • Jakob Bieling

                #8
                Re: Changing access specifier for virtual function

                Tejas Kokje <binarysemaphor e@gmail.com> wrote:
                [color=blue]
                > dragoncoder wrote:[/color]
                [color=blue][color=green]
                >> Running this program produces following output.
                >>
                >> $ ./a.out
                >> Derived
                >> $
                >>
                >> I have 2 questions:
                >> 1. Is the above code legal ?[/color][/color]
                [color=blue]
                > Since the above code compiled, it is syntactically legal.[/color]

                No. Most compilers are broken in one respect or the other. I do not
                know if Comeau is 100% compliant even. The code below will compile with
                some compilers, but it is *not* syntactically legal:

                class foo
                {
                void bar () {}
                };

                int main ()
                {
                void (foo::*pbar) () = foo::bar;
                }
                regards
                --
                jb

                (reply address in rot13, unscramble first)


                Comment

                • Tejas Kokje

                  #9
                  Re: Changing access specifier for virtual function

                  Can you tell me which compiler compiles above code ?

                  since bar() is private and non-static to class foo, how can you access
                  it outside the class using class scope ?

                  Tejas Kokje

                  Comment

                  • Daniel T.

                    #10
                    Re: Changing access specifier for virtual function

                    In article <1144433544.588 184.175570@g10g 2000cwb.googleg roups.com>,
                    "dragoncode r" <pktiwary@gmail .com> wrote:
                    [color=blue]
                    > Consider the following code
                    >
                    > #include <iostream>
                    >
                    > class Base {
                    > public:
                    > virtual void say() { std::cout << "Base" << std::endl; }
                    > };
                    >
                    > class Derived: public base {
                    > private:
                    > void say() { std::cout << "Derived" << std::endl; }
                    > };
                    >
                    > int main() {
                    > Derived d;
                    > Base* b = &d;
                    > b->say();
                    > return 0;
                    > }
                    >
                    > Running this program produces following output.
                    >
                    > $ ./a.out
                    > Derived
                    > $
                    >
                    > I have 2 questions:
                    > 1. Is the above code legal ?[/color]

                    Absolutely. It's a great way to make sure the derived object is used
                    through a base pointer.

                    [color=blue]
                    > 2. If the virtual function is private in Base, can I make it public in
                    > Derived ?[/color]

                    Yes.


                    --
                    Magic depends on tradition and belief. It does not welcome observation,
                    nor does it profit by experiment. On the other hand, science is based
                    on experience; it is open to correction by observation and experiment.

                    Comment

                    • Daniel T.

                      #11
                      Re: Changing access specifier for virtual function

                      In article <1144440608.455 649.87460@i40g2 000cwc.googlegr oups.com>,
                      "Tejas Kokje" <binarysemaphor e@gmail.com> wrote:
                      [color=blue]
                      > Can you tell me which compiler compiles above code ?
                      >
                      > since bar() is private and non-static to class foo, how can you access
                      > it outside the class using class scope ?[/color]

                      One of the compilers I use will in fact compile the code despite the
                      fact that bar() is private and non-static to class foo.

                      It's used in NintendoDS development. It also can't handle exceptions or
                      the STL.


                      --
                      Magic depends on tradition and belief. It does not welcome observation,
                      nor does it profit by experiment. On the other hand, science is based
                      on experience; it is open to correction by observation and experiment.

                      Comment

                      • Thomas Tutone

                        #12
                        Re: Changing access specifier for virtual function

                        Victor Bazarov wrote:
                        [color=blue]
                        > Thomas Tutone wrote:[/color]
                        [color=blue][color=green]
                        > > dragoncoder wrote:[/color][/color]

                        [snip]
                        [color=blue][color=green][color=darkred]
                        > >> class Base {
                        > >> public:
                        > >> virtual void say() { std::cout << "Base" << std::endl; }
                        > >> };
                        > >>
                        > >> class Derived: public base {
                        > >> private:
                        > >> void say() { std::cout << "Derived" << std::endl; }
                        > >> };[/color][/color][/color]
                        [color=blue][color=green][color=darkred]
                        > >> I have 2 questions:
                        > >> 1. Is the above code legal ?[/color]
                        > >
                        > > Yes. And an excellent example of poor coding practice.[/color]
                        >
                        > Why do you call this practice "poor"?[/color]

                        Because it violates substitutabilit y. Derived supposedly "is-a" Base,
                        and yet say() is no longer a public member function. See, for example,
                        [Cline et al., C++ FAQs (2nd Ed. 1999), FAQ 7.06 ("Is it proper to
                        revoke (hide) an inherited public member function?")].
                        [color=blue][color=green][color=darkred]
                        > >> 2. If the virtual function is private in Base, can I make it public
                        > >> in Derived ?[/color]
                        > >
                        > > No. If it were private in Base, then Derived wouldn't inherit it.[/color]
                        >
                        > Nothing is inherited here. It's _overridden_.[/color]

                        Yes, you're right - I confused my terminology. Thank you for the
                        correction.

                        Best regards,

                        Tom

                        Comment

                        • Victor Bazarov

                          #13
                          Re: Changing access specifier for virtual function

                          Thomas Tutone wrote:[color=blue]
                          > Victor Bazarov wrote:
                          >[color=green]
                          >> Thomas Tutone wrote:[/color]
                          >[color=green][color=darkred]
                          >>> dragoncoder wrote:[/color][/color]
                          >
                          > [snip]
                          >[color=green][color=darkred]
                          >>>> class Base {
                          >>>> public:
                          >>>> virtual void say() { std::cout << "Base" << std::endl; }
                          >>>> };
                          >>>>
                          >>>> class Derived: public base {
                          >>>> private:
                          >>>> void say() { std::cout << "Derived" << std::endl; }
                          >>>> };[/color][/color]
                          >[color=green][color=darkred]
                          >>>> I have 2 questions:
                          >>>> 1. Is the above code legal ?
                          >>>
                          >>> Yes. And an excellent example of poor coding practice.[/color]
                          >>
                          >> Why do you call this practice "poor"?[/color]
                          >
                          > Because it violates substitutabilit y. Derived supposedly "is-a" Base,
                          > and yet say() is no longer a public member function. See, for
                          > example, [Cline et al., C++ FAQs (2nd Ed. 1999), FAQ 7.06 ("Is it
                          > proper to revoke (hide) an inherited public member function?")].[/color]

                          I don't have that book, if you care to discuss issues brough up in it,
                          quote it, and we will know what you're talking about.

                          If the client (user) code only knows about the Base class (and only cares
                          about the Base class), changing the derived class' virtual members' access
                          specifiers (a) does not break the polymorphic behaviour and (b) prevents
                          accidental non-polymorphic use of those functions where a call to Base is
                          intended. The compiler does not make decisions to resolve the call based
                          on the access specifiers, but if it instead of intended use of Base member
                          attempts to use Derived's member, the privateness of the Derived's member
                          will cause the compiler to barf.

                          Besides, an even better practice would actually be to hide _both_ 'say'
                          members in the private area, define another, non-virtual function in Base
                          and make it call 'say':

                          class Base {
                          virtual void say();
                          public:
                          void talk() { say{}; }
                          };

                          In this case nobody should complain about changing the access levels of
                          the interface components.

                          V
                          --
                          Please remove capital As from my address when replying by mail


                          Comment

                          • Daniel T.

                            #14
                            Re: Changing access specifier for virtual function

                            In article <1144521626.944 409.229560@j33g 2000cwa.googleg roups.com>,
                            "Thomas Tutone" <Thomas8675309@ yahoo.com> wrote:
                            [color=blue]
                            > Victor Bazarov wrote:
                            >[color=green]
                            > > Thomas Tutone wrote:[/color]
                            >[color=green][color=darkred]
                            > > > dragoncoder wrote:[/color][/color]
                            >
                            > [snip]
                            >[color=green][color=darkred]
                            > > >> class Base {
                            > > >> public:
                            > > >> virtual void say() { std::cout << "Base" << std::endl; }
                            > > >> };
                            > > >>
                            > > >> class Derived: public base {
                            > > >> private:
                            > > >> void say() { std::cout << "Derived" << std::endl; }
                            > > >> };[/color][/color]
                            >[color=green][color=darkred]
                            > > >> I have 2 questions:
                            > > >> 1. Is the above code legal ?
                            > > >
                            > > > Yes. And an excellent example of poor coding practice.[/color]
                            > >
                            > > Why do you call this practice "poor"?[/color]
                            >
                            > Because it violates substitutabilit y. Derived supposedly "is-a" Base,
                            > and yet say() is no longer a public member function. See, for example,
                            > [Cline et al., C++ FAQs (2nd Ed. 1999), FAQ 7.06 ("Is it proper to
                            > revoke (hide) an inherited public member function?")].[/color]

                            also at
                            <http://www.parashift.c om/c++-faq-lite/proper-inheritance.htm l#faq-21.1>
                            Attempting to hide (eliminate, revoke, privatize) inherited public
                            member functions is an all-too-common design error. It usually stems
                            from muddy thinking.

                            In the above code, Derived does not eliminate, revoke or privatize
                            anything. Any user of Derived can still call the 'say' function (by
                            using a Base*.)

                            Unfortunately, Cline doesn't give an example of what he means (either on
                            the website or in the book.)


                            --
                            Magic depends on tradition and belief. It does not welcome observation,
                            nor does it profit by experiment. On the other hand, science is based
                            on experience; it is open to correction by observation and experiment.

                            Comment

                            Working...