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. The Lounge
  3. A programming question

A programming question

Scheduled Pinned Locked Moved The Lounge
questioncsharpc++comoop
37 Posts 17 Posters 65 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
    David ONeil
    wrote on last edited by
    #1

    Disregarding the flagrant violation of protocol, and the total disregard of the red text at the top of the forum, those of you who have programmed in C++, have you ever used protected and private inheritance? I've never had a use for it, but have been extremely curious about when they are handy. They are not in C# (from what I've read), but maybe I've overlooked something, and they are useful in ways I don't know? Your thoughts and experiences, to expand an undeveloped understanding...

    The forgotten roots of science | C++ Programming | DWinLib

    CPalliniC J Kornfeld Eliyahu PeterK L D 14 Replies Last reply
    0
    • D David ONeil

      Disregarding the flagrant violation of protocol, and the total disregard of the red text at the top of the forum, those of you who have programmed in C++, have you ever used protected and private inheritance? I've never had a use for it, but have been extremely curious about when they are handy. They are not in C# (from what I've read), but maybe I've overlooked something, and they are useful in ways I don't know? Your thoughts and experiences, to expand an undeveloped understanding...

      The forgotten roots of science | C++ Programming | DWinLib

      CPalliniC Online
      CPalliniC Online
      CPallini
      wrote on last edited by
      #2

      I never used protected or private inheritance. Anyway, I reckon they could be useful.

      In testa che avete, signor di Ceprano?

      D 1 Reply Last reply
      0
      • CPalliniC CPallini

        I never used protected or private inheritance. Anyway, I reckon they could be useful.

        D Offline
        D Offline
        David ONeil
        wrote on last edited by
        #3

        The first time I read about them (years ago), they made no sense to me. The last reading I did made them seem like protected inheritance made public members protected, and private inheritance made public members private in the derived class. I can see this maybe being useful in some situations, but am curious as to where that would be the case.

        The forgotten roots of science | C++ Programming | DWinLib

        CPalliniC J 2 Replies Last reply
        0
        • D David ONeil

          The first time I read about them (years ago), they made no sense to me. The last reading I did made them seem like protected inheritance made public members protected, and private inheritance made public members private in the derived class. I can see this maybe being useful in some situations, but am curious as to where that would be the case.

          The forgotten roots of science | C++ Programming | DWinLib

          CPalliniC Online
          CPalliniC Online
          CPallini
          wrote on last edited by
          #4

          I suppose they could have use, for instance, in implementing a Facade design pattern.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • D David ONeil

            Disregarding the flagrant violation of protocol, and the total disregard of the red text at the top of the forum, those of you who have programmed in C++, have you ever used protected and private inheritance? I've never had a use for it, but have been extremely curious about when they are handy. They are not in C# (from what I've read), but maybe I've overlooked something, and they are useful in ways I don't know? Your thoughts and experiences, to expand an undeveloped understanding...

            The forgotten roots of science | C++ Programming | DWinLib

            J Offline
            J Offline
            Jon McKee
            wrote on last edited by
            #5

            I'm not a regular C++ dev but I've never had a reason to use them so far. I've also never seen a good example of why they're useful that can't be solved in another way (e.g. composition). Whether you value it or not is up to you and the situation, but it's worth mentioning that private and protected inheritance breaks the Liskov Substitution Principle, and private inheritance breaks polymorphism in the inheritance hierarchy. Honestly though, I'd love to see an example of either being useful.

            1 Reply Last reply
            0
            • D David ONeil

              Disregarding the flagrant violation of protocol, and the total disregard of the red text at the top of the forum, those of you who have programmed in C++, have you ever used protected and private inheritance? I've never had a use for it, but have been extremely curious about when they are handy. They are not in C# (from what I've read), but maybe I've overlooked something, and they are useful in ways I don't know? Your thoughts and experiences, to expand an undeveloped understanding...

              The forgotten roots of science | C++ Programming | DWinLib

              Kornfeld Eliyahu PeterK Offline
              Kornfeld Eliyahu PeterK Offline
              Kornfeld Eliyahu Peter
              wrote on last edited by
              #6

              You maybe used composition to define 'has-a' relations... that's the way C# preferred, so no private inheritance there...

              "The only place where Success comes before Work is in the dictionary." Vidal Sassoon, 1928 - 2012

              "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

              1 Reply Last reply
              0
              • D David ONeil

                Disregarding the flagrant violation of protocol, and the total disregard of the red text at the top of the forum, those of you who have programmed in C++, have you ever used protected and private inheritance? I've never had a use for it, but have been extremely curious about when they are handy. They are not in C# (from what I've read), but maybe I've overlooked something, and they are useful in ways I don't know? Your thoughts and experiences, to expand an undeveloped understanding...

                The forgotten roots of science | C++ Programming | DWinLib

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Well, If you ever get to work at a company with thousands of developers (or as a program manager) you'll quickly understand the value. They are a very useful tool for controlling the application binary interface[^]. Let's say that you create an awesome library called DoSomethingAwesome.lib and everybody at your company wants to use DoSomethingAwesome. They take your lib as a dependency and quickly integrate it into dozens of projects. They derive classes from your headers and extend them and now it's DoSomethingMoreAwesome. Then your team changes some of the internals and DoSomethingAwesome is no longer compatible with DoSomethingMoreAwesome. In fact now dozens of other teams at the company are having to re-write parts of their code to accommodate for the changes in your lib. Paying software engineers is expensive and now this change has cost the company thousands of dollars. The concept of protected and private inheritance is a useful tool that allows the library development team to control the public interfaces and keep a stable ABI. It's just a tool in the toolbox. Best Wishes, -David Delaune

                D S 2 Replies Last reply
                0
                • D David ONeil

                  The first time I read about them (years ago), they made no sense to me. The last reading I did made them seem like protected inheritance made public members protected, and private inheritance made public members private in the derived class. I can see this maybe being useful in some situations, but am curious as to where that would be the case.

                  The forgotten roots of science | C++ Programming | DWinLib

                  J Offline
                  J Offline
                  Jon McKee
                  wrote on last edited by
                  #8

                  All members of the base class get restricted to the inheritance type. So both public and protected members of the base class are now private in the derived class if using private inheritance. So any users of that derived class don't "see" the inheritance. As I said in my other post, I've never seen a good example of why this would be needed though :confused: EDIT: Two words for clarity. Also private base class members are still not visible to the derived class iirc.

                  1 Reply Last reply
                  0
                  • D David ONeil

                    Disregarding the flagrant violation of protocol, and the total disregard of the red text at the top of the forum, those of you who have programmed in C++, have you ever used protected and private inheritance? I've never had a use for it, but have been extremely curious about when they are handy. They are not in C# (from what I've read), but maybe I've overlooked something, and they are useful in ways I don't know? Your thoughts and experiences, to expand an undeveloped understanding...

                    The forgotten roots of science | C++ Programming | DWinLib

                    D Offline
                    D Offline
                    Daniel Pfeffer
                    wrote on last edited by
                    #9

                    You use inheritance when a derived type must implement the functionality of the base type. * Public inheritance is used when the public functionality of the base type may be used by anyone. * Protected inheritance is used when the public functionality of the base type may be used only by further-derived types. * Private inheritance is used when the public functionality of the base type may not be used by further-derived types. An example for private (or protected) inheritance might be a wrapper for the HANDLE object in Windows. A HANDLE wrapper may be constructed directly so its constructor must be public, but inside a File object you probably want only the File object (or its derived types) to be able to access the HANDLE.

                    Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                    1 Reply Last reply
                    0
                    • L Lost User

                      Well, If you ever get to work at a company with thousands of developers (or as a program manager) you'll quickly understand the value. They are a very useful tool for controlling the application binary interface[^]. Let's say that you create an awesome library called DoSomethingAwesome.lib and everybody at your company wants to use DoSomethingAwesome. They take your lib as a dependency and quickly integrate it into dozens of projects. They derive classes from your headers and extend them and now it's DoSomethingMoreAwesome. Then your team changes some of the internals and DoSomethingAwesome is no longer compatible with DoSomethingMoreAwesome. In fact now dozens of other teams at the company are having to re-write parts of their code to accommodate for the changes in your lib. Paying software engineers is expensive and now this change has cost the company thousands of dollars. The concept of protected and private inheritance is a useful tool that allows the library development team to control the public interfaces and keep a stable ABI. It's just a tool in the toolbox. Best Wishes, -David Delaune

                      D Offline
                      D Offline
                      David ONeil
                      wrote on last edited by
                      #10

                      Thank you Very Much for this explanation! As I said before, I am in awe of your talents, and this is helpful for my understanding!

                      The forgotten roots of science | C++ Programming | DWinLib

                      L 1 Reply Last reply
                      0
                      • D David ONeil

                        Thank you Very Much for this explanation! As I said before, I am in awe of your talents, and this is helpful for my understanding!

                        The forgotten roots of science | C++ Programming | DWinLib

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Well, I dunno man, when I read this headline the other day I was almost certain they were referring to software engineers. America is facing a monkey shortage[^] Best Wishes, -🐵🍌

                        D 1 Reply Last reply
                        0
                        • D David ONeil

                          Disregarding the flagrant violation of protocol, and the total disregard of the red text at the top of the forum, those of you who have programmed in C++, have you ever used protected and private inheritance? I've never had a use for it, but have been extremely curious about when they are handy. They are not in C# (from what I've read), but maybe I've overlooked something, and they are useful in ways I don't know? Your thoughts and experiences, to expand an undeveloped understanding...

                          The forgotten roots of science | C++ Programming | DWinLib

                          R Offline
                          R Offline
                          Rage
                          wrote on last edited by
                          #12

                          Yes, they come very handy to define the degree of derivation freedom you want to let users for your interface elements. Why the question ?

                          Do not escape reality : improve reality !

                          D 1 Reply Last reply
                          0
                          • R Rage

                            Yes, they come very handy to define the degree of derivation freedom you want to let users for your interface elements. Why the question ?

                            Do not escape reality : improve reality !

                            D Offline
                            D Offline
                            David ONeil
                            wrote on last edited by
                            #13

                            Rage wrote:

                            Why the question ?

                            Because I have never seen good examples for public and private derivation.

                            The forgotten roots of science | C++ Programming | DWinLib

                            R 1 Reply Last reply
                            0
                            • L Lost User

                              Well, I dunno man, when I read this headline the other day I was almost certain they were referring to software engineers. America is facing a monkey shortage[^] Best Wishes, -🐵🍌

                              D Offline
                              D Offline
                              David ONeil
                              wrote on last edited by
                              #14

                              The abbreviation is overused, but lol!

                              The forgotten roots of science | C++ Programming | DWinLib

                              D 1 Reply Last reply
                              0
                              • D David ONeil

                                Rage wrote:

                                Why the question ?

                                Because I have never seen good examples for public and private derivation.

                                The forgotten roots of science | C++ Programming | DWinLib

                                R Offline
                                R Offline
                                Rage
                                wrote on last edited by
                                #15

                                Oh. I was hoping for a more crunchy story with secret services and hacking passwords.

                                Do not escape reality : improve reality !

                                D 1 Reply Last reply
                                0
                                • R Rage

                                  Oh. I was hoping for a more crunchy story with secret services and hacking passwords.

                                  Do not escape reality : improve reality !

                                  D Offline
                                  D Offline
                                  David ONeil
                                  wrote on last edited by
                                  #16

                                  Sorry to disappoint you, but I am a mere mortal programmer. :laugh: I don't have time to brute force passwords and such...

                                  The forgotten roots of science | C++ Programming | DWinLib

                                  R 1 Reply Last reply
                                  0
                                  • D David ONeil

                                    Sorry to disappoint you, but I am a mere mortal programmer. :laugh: I don't have time to brute force passwords and such...

                                    The forgotten roots of science | C++ Programming | DWinLib

                                    R Offline
                                    R Offline
                                    Rage
                                    wrote on last edited by
                                    #17

                                    That's because you do not use private and protected inheritance... :laugh:

                                    Do not escape reality : improve reality !

                                    D 1 Reply Last reply
                                    0
                                    • R Rage

                                      That's because you do not use private and protected inheritance... :laugh:

                                      Do not escape reality : improve reality !

                                      D Offline
                                      D Offline
                                      David ONeil
                                      wrote on last edited by
                                      #18

                                      +10! Made me laugh!

                                      The forgotten roots of science | C++ Programming | DWinLib

                                      D 1 Reply Last reply
                                      0
                                      • D David ONeil

                                        +10! Made me laugh!

                                        The forgotten roots of science | C++ Programming | DWinLib

                                        D Offline
                                        D Offline
                                        David ONeil
                                        wrote on last edited by
                                        #19

                                        (And also made me a bit sad, that we are overwriting the lives of other species to supposedly improve our own.)

                                        The forgotten roots of science | C++ Programming | DWinLib

                                        1 Reply Last reply
                                        0
                                        • D David ONeil

                                          Disregarding the flagrant violation of protocol, and the total disregard of the red text at the top of the forum, those of you who have programmed in C++, have you ever used protected and private inheritance? I've never had a use for it, but have been extremely curious about when they are handy. They are not in C# (from what I've read), but maybe I've overlooked something, and they are useful in ways I don't know? Your thoughts and experiences, to expand an undeveloped understanding...

                                          The forgotten roots of science | C++ Programming | DWinLib

                                          S Offline
                                          S Offline
                                          Shao Voon Wong
                                          wrote on last edited by
                                          #20

                                          I have used private inheritance. The base class public access members are still public access in the derived class. Therefore the derived class still can call the base class functions and access its public data. But the user who instantiates the derived class, cannot access the public member of the base class, hence private inheritance. Public inheritance is a "is-a" relationship. Private inheritance is a "implemented-in-terms-of" relationship. A useful example, is I like .NET string class and like a C++ string class with the same C# methods but I do not want to reimplement from scratch, so I derived from std::wstring with private inheritance to make use of its functionality, so that user of my string class cannot access the base class's std::wstring to avoid the confusion.

                                          class MyString : private std::wstring
                                          {
                                          };

                                          There is an excellent blog about this topic: [C++ Tutorial: Private Inheritance - 2020](https://www.bogotobogo.com/cplusplus/private\_inheritance.php)

                                          D 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