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 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
          • D David ONeil

            The abbreviation is overused, but lol!

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

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

            (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

            L 1 Reply Last reply
            0
            • S Shao Voon Wong

              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 Offline
              D Offline
              David ONeil
              wrote on last edited by
              #22

              Thank you. Very helpful!

              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

                A Offline
                A Offline
                Arthur V Ratz
                wrote on last edited by
                #23

                Hi David, According to this, private inheritance in C++17 is primarily used whenever you're having a need to make all those public and protected members, in a base class, to become private in all derived descendant classes, and, thus not accessible via a derived class object. Whist, according to this, all public and protected members of a base class become protected in all derived classes. This is typically needed to make those public members of a base class to become protected in all descendent classes, rather than those methods are accessible via a derived class object.

                1 Reply Last reply
                0
                • D David ONeil

                  (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

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

                  Yeah,

                  David O'Neil wrote:

                  we are overwriting the lives of other species to supposedly improve our own

                  One of my old coworkers has a wife that was working as a research scientist at the Tulane National Primate Research Center |[^] and she would describe some of her research. They have over 5,000 primates, you have absolutely no idea... what she was telling me was worse than anything you can possibly even imagine. Best Wishes, -David Delaune

                  W D 2 Replies 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

                    S Offline
                    S Offline
                    Stefan_Lang
                    wrote on last edited by
                    #25

                    A good example, but most cases I've come across caring about these kind of problems use delegates instead of inheritance. Delegates come with the additional advantage to keep what would otherwise be the base class entirely out of the API (i. e. the headers). I. e. instead of putting this into your header:

                    class MyEncapsulatedBaseClass { // ...
                    public:
                    void doAwesomeStuff();
                    };
                    class MYAPI MyDerivedClass : private MyEncapsulatedBaseClass { // ...
                    public:
                    void doMoreAwsomeStuff(); // calls doAwesomeStuff() and more
                    };

                    you only put this in your header:

                    class MYAPI MyClass {
                    class MyEncapsulatedClass* delegate;
                    public:
                    void doMoreAwesomeStuff(); // uses delegate to do awesome stuff and then does more
                    };

                    The advantage of the latter is that you can switch to a different encapsulated class or change the function and data members in that class without affecting the API delivered to your clients. The only advantage of the former I can think of is, that with protected inheritance, that protection is not final, at least not for virtual member functions: another derived class can overide any protected virutal base function with a public one, making it accessible again! (Of course, if you want to retain that option, the question is why don't you use public inheritance to start with ? :wtf: )

                    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                    L 1 Reply Last reply
                    0
                    • S Stefan_Lang

                      A good example, but most cases I've come across caring about these kind of problems use delegates instead of inheritance. Delegates come with the additional advantage to keep what would otherwise be the base class entirely out of the API (i. e. the headers). I. e. instead of putting this into your header:

                      class MyEncapsulatedBaseClass { // ...
                      public:
                      void doAwesomeStuff();
                      };
                      class MYAPI MyDerivedClass : private MyEncapsulatedBaseClass { // ...
                      public:
                      void doMoreAwsomeStuff(); // calls doAwesomeStuff() and more
                      };

                      you only put this in your header:

                      class MYAPI MyClass {
                      class MyEncapsulatedClass* delegate;
                      public:
                      void doMoreAwesomeStuff(); // uses delegate to do awesome stuff and then does more
                      };

                      The advantage of the latter is that you can switch to a different encapsulated class or change the function and data members in that class without affecting the API delivered to your clients. The only advantage of the former I can think of is, that with protected inheritance, that protection is not final, at least not for virtual member functions: another derived class can overide any protected virutal base function with a public one, making it accessible again! (Of course, if you want to retain that option, the question is why don't you use public inheritance to start with ? :wtf: )

                      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

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

                      Yep, C++ has become such a feature-rich language. There are many tools in the programmers toolbox. Best Wishes, -David Delaune

                      1 Reply Last reply
                      0
                      • L Lost User

                        Yeah,

                        David O'Neil wrote:

                        we are overwriting the lives of other species to supposedly improve our own

                        One of my old coworkers has a wife that was working as a research scientist at the Tulane National Primate Research Center |[^] and she would describe some of her research. They have over 5,000 primates, you have absolutely no idea... what she was telling me was worse than anything you can possibly even imagine. Best Wishes, -David Delaune

                        W Offline
                        W Offline
                        W Balboos GHB
                        wrote on last edited by
                        #27

                        All one needs to do is recall images of live monkeys with the tops of their skulls removed and numerous wires implanted. There's a reason that there are groups who are outraged by these practices are sometimes even provoked to mass-releases by attacking the labs. Those who condemn them - maybe if they had a look first, before the lab covered up the less PR-improving experiments. Not just monkeys - much of animal testing is irrelevant. If we relied on its outcome, we'd consider aspiring toxic (cats) and chocolate toxic (dogs) - we'd make up for it with amanita phalloides as a tasty addition to our diet since it doesn't harm rabbits. That's a mushroom more commonly called "The Death Angel".

                        Ravings en masse^

                        "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                        "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                        L 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

                          N Offline
                          N Offline
                          Nemanja Trifunovic
                          wrote on last edited by
                          #28

                          One instance where I used private inheritance in pre C++ 11 days was to easily make a class non-copyable:

                          class fancy_type : private boost::noncopyable
                          {};

                          utf8-cpp

                          1 Reply Last reply
                          0
                          • W W Balboos GHB

                            All one needs to do is recall images of live monkeys with the tops of their skulls removed and numerous wires implanted. There's a reason that there are groups who are outraged by these practices are sometimes even provoked to mass-releases by attacking the labs. Those who condemn them - maybe if they had a look first, before the lab covered up the less PR-improving experiments. Not just monkeys - much of animal testing is irrelevant. If we relied on its outcome, we'd consider aspiring toxic (cats) and chocolate toxic (dogs) - we'd make up for it with amanita phalloides as a tasty addition to our diet since it doesn't harm rabbits. That's a mushroom more commonly called "The Death Angel".

                            Ravings en masse^

                            "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                            "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

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

                            W∴ Balboos, GHB wrote:

                            All one needs to do is recall images of live monkeys with the tops of their skulls removed and numerous wires implanted.

                            I don't know how you knew that, but she also told me that the experiments required that the primates were conscious to get accurate data. I don't know how she could sleep at night after doing something like that. It's disgusting. Best Wishes, -David Delaune

                            W 1 Reply Last reply
                            0
                            • L Lost User

                              W∴ Balboos, GHB wrote:

                              All one needs to do is recall images of live monkeys with the tops of their skulls removed and numerous wires implanted.

                              I don't know how you knew that, but she also told me that the experiments required that the primates were conscious to get accurate data. I don't know how she could sleep at night after doing something like that. It's disgusting. Best Wishes, -David Delaune

                              W Offline
                              W Offline
                              W Balboos GHB
                              wrote on last edited by
                              #30

                              Randor wrote:

                              I don't know how you knew that,

                              Pictures of that have been available for years (often showing up in donation requests from animal rights groups). Animals deliberately crippled, typically spinal cord damage, for experiments. Vivisection is common. Personally, I won't even wear leather as it's value-added to the slaughter-industry (and rather repulsive if you think about what it is).

                              Ravings en masse^

                              "The difference between genius and stupidity is that genius has its limits." - Albert Einstein

                              "If you are searching for perfection in others, then you seek disappointment. If you seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010

                              1 Reply Last reply
                              0
                              • L Lost User

                                Yeah,

                                David O'Neil wrote:

                                we are overwriting the lives of other species to supposedly improve our own

                                One of my old coworkers has a wife that was working as a research scientist at the Tulane National Primate Research Center |[^] and she would describe some of her research. They have over 5,000 primates, you have absolutely no idea... what she was telling me was worse than anything you can possibly even imagine. Best Wishes, -David Delaune

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

                                A more positive story about wildlife: Eagle's Nest Township, Minnesota had/(has?) a different relationship with bears. If you don't want to listen to a program, Salon did an article on him as well.

                                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

                                  R Offline
                                  R Offline
                                  RustyF
                                  wrote on last edited by
                                  #32

                                  At the end of the day, it’s a way to implement your public interface to the world without your clients being exposed to the details. You have many options to implement that interface; code directly in the class, delegating to others (composition) or inheriting it. Imagine parallel universes, there is the public one that clients exist in and another that the non-public stuff lives in. Both (potentially) have a need for inheritance either to represent a natural taxonomy (CheckingAccount isa Account, etc) or for reuse (Account inherits Persistent, Equatable). I say “potentially” because out of all the ways to implement your public interface, inheritance is by far the worst in my opinion - it’s the road to spaghetti code ;).

                                  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

                                    G Offline
                                    G Offline
                                    giulicard
                                    wrote on last edited by
                                    #33

                                    Basically, public inheritance is an "IS-A" relationship while protected or private inheritance is a "HAS-A" relationship. A "HAS-A" relationship is like aggregation.

                                    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
                                      Stuart Dootson
                                      wrote on last edited by
                                      #34

                                      The only time I've used it is when deriving from a class to make use of its functionality, but not wanting to make all of the public base class members accessible. For example, making a fixed vector class that you can iterate over: ```c++ #include #include template struct fixed_vector : private std::vector { using std::vector::vector; using std::vector::begin; using std::vector::end; }; int main() { auto f = fixed_vector{1,2, 3,4 ,5}; for(const auto i : f) { std::cout << i << std::endl; } f.push_back(10); // Causes compiler error } ```

                                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                                      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

                                        P Offline
                                        P Offline
                                        patbob
                                        wrote on last edited by
                                        #35

                                        David O'Neil wrote:

                                        have you ever used protected and private inheritance?

                                        All the time. Public is for the public parts of the interface to the class. The others are for the internal implementation details -- the parts we want to be free to rewrite and refactor without consequence. In my experience, a bigger public interface to a class means tighter coupling over time since other developers don't have to think very carefully about how their usage tightens the coupling, so they don't and just go ahead and use it.

                                        5G -- more lies faster.

                                        D 1 Reply Last reply
                                        0
                                        • P patbob

                                          David O'Neil wrote:

                                          have you ever used protected and private inheritance?

                                          All the time. Public is for the public parts of the interface to the class. The others are for the internal implementation details -- the parts we want to be free to rewrite and refactor without consequence. In my experience, a bigger public interface to a class means tighter coupling over time since other developers don't have to think very carefully about how their usage tightens the coupling, so they don't and just go ahead and use it.

                                          5G -- more lies faster.

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

                                          That is a great response, that makes me see things in a new light! Thanks! It is especially helpful breaking free from the 'aggregation/inheritance' argument, that never really made any sense to me since private inheritance is NOT aggregation!

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

                                          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