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. inheritance question

inheritance question

Scheduled Pinned Locked Moved C / C++ / MFC
questionoophelpannouncementlearning
13 Posts 4 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.
  • L lune12

    Hi, I need to update a code that was written a long time ago, and have some problem. my question is as follow. I have a Base class with function f1 that do something. the Derived class ovveride function f1 and do other thing. now I need to add to the derived class a function f2 that do exactly what Base::f1() is doing. and I dont want to duplicate the code (of course) is it OK to do:

    void Derived::f2()
    {
    Base::f1();
    }

    or is there a better way to call the f1 from the base class? Many thanks,

    A Offline
    A Offline
    Aescleal
    wrote on last edited by
    #3

    You don't actually have to implement a function to call a base class version of a function: Derived d; d.base::f(); will call the base class version of f - no virtual function dispatch will happen. Oh, don't try this the other way around - strange and destructive things will happen :-) Cheers, Ash

    N 1 Reply Last reply
    0
    • A Aescleal

      You don't actually have to implement a function to call a base class version of a function: Derived d; d.base::f(); will call the base class version of f - no virtual function dispatch will happen. Oh, don't try this the other way around - strange and destructive things will happen :-) Cheers, Ash

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #4

      Actually there are scenarios where this is useful, like exposing methods from private and protected inheritance. A sort of forwarding.

      class Base
      {
      public:
      string name() const;
      };
      class Derived : protected Base
      {
      public:
      string name() const { return Base::name(); }
      };

      Edit: The reason being of course to change the access level of a method.

      home

      M A 2 Replies Last reply
      0
      • N Niklas L

        Actually there are scenarios where this is useful, like exposing methods from private and protected inheritance. A sort of forwarding.

        class Base
        {
        public:
        string name() const;
        };
        class Derived : protected Base
        {
        public:
        string name() const { return Base::name(); }
        };

        Edit: The reason being of course to change the access level of a method.

        home

        M Offline
        M Offline
        Moak
        wrote on last edited by
        #5

        Just a thought: As someone who prefers composition over inheritance, I can hardly think of a case using protected/private inheritance. Not sure when I used it last time. Also see Stackoverflow[^]. Cheers :) /Moak

        Chat in Europe :java: Now with 24% more Twitter

        A N 2 Replies Last reply
        0
        • M Moak

          Just a thought: As someone who prefers composition over inheritance, I can hardly think of a case using protected/private inheritance. Not sure when I used it last time. Also see Stackoverflow[^]. Cheers :) /Moak

          Chat in Europe :java: Now with 24% more Twitter

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #6

          It's more likely to be used to open up a protected function in the base class. I've seen that a lot in MFC code and it gives me the willies:

          class base
          {
          protected:
          virtual void a();
          };

          class derived : public base
          {
          public:
          virtual void b()
          {
          a();
          }
          };

          You can also do it using using declarations but that's scary as well. Cheers, Ash

          M 1 Reply Last reply
          0
          • N Niklas L

            Actually there are scenarios where this is useful, like exposing methods from private and protected inheritance. A sort of forwarding.

            class Base
            {
            public:
            string name() const;
            };
            class Derived : protected Base
            {
            public:
            string name() const { return Base::name(); }
            };

            Edit: The reason being of course to change the access level of a method.

            home

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #7

            In this case I'd almost certainly rewrite the base class to provide what I want than hack the derived class. I know there are social reasons why it may be preferable to forward calls like that but it leaves a bad taste in my mouth. Actually the real bad taste is implementation inheritance but in the world of GUI frameworks people are very stuck in the early 90s. Cheers, Ash

            N 1 Reply Last reply
            0
            • M Moak

              Just a thought: As someone who prefers composition over inheritance, I can hardly think of a case using protected/private inheritance. Not sure when I used it last time. Also see Stackoverflow[^]. Cheers :) /Moak

              Chat in Europe :java: Now with 24% more Twitter

              N Offline
              N Offline
              Niklas L
              wrote on last edited by
              #8

              I have actually never inherited in any other way than public as far as I can recall, but elevating the access level of a function does not need protected or private inheritance though.

              home

              1 Reply Last reply
              0
              • A Aescleal

                In this case I'd almost certainly rewrite the base class to provide what I want than hack the derived class. I know there are social reasons why it may be preferable to forward calls like that but it leaves a bad taste in my mouth. Actually the real bad taste is implementation inheritance but in the world of GUI frameworks people are very stuck in the early 90s. Cheers, Ash

                N Offline
                N Offline
                Niklas L
                wrote on last edited by
                #9

                Unfortunately, far from all classes can be rewritten (for several reasons other than social) I have used this for access level elevation a couple of times (probably more than two) in about 17 years, so it's not really common, but I believe there is a use for it. There are of course also other reasons to implement a method in terms of the base class implementation. Code generation tools does it all the time. Mostly along with ToDo comments.

                home

                A 1 Reply Last reply
                0
                • A Aescleal

                  It's more likely to be used to open up a protected function in the base class. I've seen that a lot in MFC code and it gives me the willies:

                  class base
                  {
                  protected:
                  virtual void a();
                  };

                  class derived : public base
                  {
                  public:
                  virtual void b()
                  {
                  a();
                  }
                  };

                  You can also do it using using declarations but that's scary as well. Cheers, Ash

                  M Offline
                  M Offline
                  Moak
                  wrote on last edited by
                  #10

                  Begs for a redesign/refactoring of the interface, if you ask me. :D

                  Chat in Europe :java: Now with 24% more Twitter

                  1 Reply Last reply
                  0
                  • N Niklas L

                    Unfortunately, far from all classes can be rewritten (for several reasons other than social) I have used this for access level elevation a couple of times (probably more than two) in about 17 years, so it's not really common, but I believe there is a use for it. There are of course also other reasons to implement a method in terms of the base class implementation. Code generation tools does it all the time. Mostly along with ToDo comments.

                    home

                    A Offline
                    A Offline
                    Aescleal
                    wrote on last edited by
                    #11

                    The only time you can't rewrite a base class is when you haven't got the code for it. The rest of the reasons are all social. Just out of interest anything you can do with inheritance (of concrete base classes) in the context of code generation you could a lot more cleanly do with composition. MFC, as one example, wouldn't be such a mess of deriving from concrete classes if they'd have done that. However there wasn't the C++ expertise around in 1991 that there is now so you can't blame them in hindsight. Cheers, Ash

                    N 1 Reply Last reply
                    0
                    • A Aescleal

                      The only time you can't rewrite a base class is when you haven't got the code for it. The rest of the reasons are all social. Just out of interest anything you can do with inheritance (of concrete base classes) in the context of code generation you could a lot more cleanly do with composition. MFC, as one example, wouldn't be such a mess of deriving from concrete classes if they'd have done that. However there wasn't the C++ expertise around in 1991 that there is now so you can't blame them in hindsight. Cheers, Ash

                      N Offline
                      N Offline
                      Niklas L
                      wrote on last edited by
                      #12

                      I think the social bit got lost in translation. I was looking upon social as political, and not economical, which I assume now you include in social?

                      home

                      A 1 Reply Last reply
                      0
                      • N Niklas L

                        I think the social bit got lost in translation. I was looking upon social as political, and not economical, which I assume now you include in social?

                        home

                        A Offline
                        A Offline
                        Aescleal
                        wrote on last edited by
                        #13

                        I tend to include economic reasons as social, although as a supposed engineer perhaps I shouldn't. Cheers, Ash

                        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