Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to hide a public function inherit from base class

How to hide a public function inherit from base class

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dracula Wang
    wrote on last edited by
    #1

    I have 2 classes, B inherit from A, but actually A have a function: fun should not present in B, so I code as blow,

    class A{
    public:
    virtual void fun(){cout << "a" << endl; };
    };

    class B : public A{
    virtual void fun(){};
    };

    But this take no effect, I can call the fun in class b,

    int main()
    {
    A a;
    B b;
    A *pa = new B;

    a.fun();
    b.A::fun();
    pa->fun();
    

    }

    the output is

    a
    a

    How to decline the function call? Thanks.

    C B 2 Replies Last reply
    0
    • D Dracula Wang

      I have 2 classes, B inherit from A, but actually A have a function: fun should not present in B, so I code as blow,

      class A{
      public:
      virtual void fun(){cout << "a" << endl; };
      };

      class B : public A{
      virtual void fun(){};
      };

      But this take no effect, I can call the fun in class b,

      int main()
      {
      A a;
      B b;
      A *pa = new B;

      a.fun();
      b.A::fun();
      pa->fun();
      

      }

      the output is

      a
      a

      How to decline the function call? Thanks.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      What is your point? pa->fun() doesn't fit your needs?

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      D 1 Reply Last reply
      0
      • C CPallini

        What is your point? pa->fun() doesn't fit your needs?

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        D Offline
        D Offline
        Dracula Wang
        wrote on last edited by
        #3

        yes, the A::fun() is bad for the B, will case error, so i want let A::fun to be public, but to B the fun must cannot be call

        C 1 Reply Last reply
        0
        • D Dracula Wang

          yes, the A::fun() is bad for the B, will case error, so i want let A::fun to be public, but to B the fun must cannot be call

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          What about (1) Writing A class without fun function. (2) Writing a (say) AA class that inherits from A and provides fun function. (3) Writing a B class that inherits from A class and therefore is not able to call fun function? Of course A class consumers should become AA class ones. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          D 1 Reply Last reply
          0
          • C CPallini

            What about (1) Writing A class without fun function. (2) Writing a (say) AA class that inherits from A and provides fun function. (3) Writing a B class that inherits from A class and therefore is not able to call fun function? Of course A class consumers should become AA class ones. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            D Offline
            D Offline
            Dracula Wang
            wrote on last edited by
            #5

            yeah, that's the better way to do, but in this project others may dislike this implement, they didn't want their code update, @_@. Thanks for all your help.

            1 Reply Last reply
            0
            • D Dracula Wang

              I have 2 classes, B inherit from A, but actually A have a function: fun should not present in B, so I code as blow,

              class A{
              public:
              virtual void fun(){cout << "a" << endl; };
              };

              class B : public A{
              virtual void fun(){};
              };

              But this take no effect, I can call the fun in class b,

              int main()
              {
              A a;
              B b;
              A *pa = new B;

              a.fun();
              b.A::fun();
              pa->fun();
              

              }

              the output is

              a
              a

              How to decline the function call? Thanks.

              B Offline
              B Offline
              BadKarma
              wrote on last edited by
              #6

              Hi, I don't understand what you don't want to happen for the B class. do you want to hide the functionaly from a b object or the declaration; like this:

              int main()
              {
               A a;
               B b;
              
               a.fun();  //  outputs "a"
               b.fun();  //  compiles but doens't do something
               // or
               b.fun();  //  doens't compile
              
               A* p = &b;
               p->fun(); //  compiles but doens't do something
              
               b.A::fun();  // compiles but doens't do something
               
              }
              

              Maybe you could use the Facade pattern to hide the unwanted A functionaly.

              Learn from the mistakes of others, you may not live long enough to make them all yourself.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups