New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Listing 5-6 #173
Comments
Thanks, @theundergroundsorcerer! I'm not sure I understand this issue. The point of Listing 5-6 is that the commented out portion does not compile because |
Except that it has nothing to do with inheritance and the discussion in the previous paragraph. I cite the whole paragraph. Derived classes inherit non-private members from their base classes. Classes can use inherited members just like normal members. The supposed benefit of member inheritance is that you can define functionality once in a base class and not have to repeat it in the derived classes. Unfortunately, experience has convinced many in the programming community to avoid member inheritance because it can easily yield brittle, hard-to-reason-about code compared to composition-based polymorphism. (This is why so many modern programming languages exclude it.) The class in Listing 5-6 illustrates member inheritance.
The answer is 42 ➎ Listing 5-6: A program using inherited members Here, BaseClass has a public method ➊, a public field ➋, and a private field ➌. You declare a DerivedClass deriving from BaseClass ➍ and then use it in main. Because they’re inherited as public members, the_answer ➎ and member ➏ are available on the DerivedClass x. However, uncommenting ➐ yields a compiler error because holistic_detective is private and thus not inherited by derived classes. The problem with this code is that compilation error has nothing to do with inheritance. |
I see the issue now, thanks. I've added this to the errata section and updated the listing. Let me know if you see any other issues! |
The code doesn't compile not because of inheritance. It would not compile because even if we used BaseClass, because holistic_directive is private value.
Please provide a better and correct example.
The text was updated successfully, but these errors were encountered: