Skip to content
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

Closed
theundergroundsorcerer opened this issue May 14, 2020 · 3 comments
Closed

Listing 5-6 #173

theundergroundsorcerer opened this issue May 14, 2020 · 3 comments

Comments

@theundergroundsorcerer
Copy link

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.

@JLospinoso
Copy link
Owner

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 holistic_detective is, as you've pointed out, private.

image

@theundergroundsorcerer
Copy link
Author

theundergroundsorcerer commented May 14, 2020

Except that it has nothing to do with inheritance and the discussion in the previous paragraph.

I cite the whole paragraph.
Member Inheritance

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.

#include <cstdio>

struct BaseClass {
  int the_answer() const { return 42; } ➊
  const char* member = "gold"; ➋
private:
  const char* holistic_detective = "Dirk Gently"; ➌
};

struct DerivedClass : BaseClass ➍ {};

int main() {
  DerivedClass x;
  printf("The answer is %d\n", x.the_answer()); ➎
  printf("%s member\n", x.member); ➏
  // This line doesn't compile:
  // printf("%s's Holistic Detective Agency\n", x.holistic_detective); ➐
}

The answer is 42 ➎
gold member ➏

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.
Has it been
int main() { BaseClass x; printf("The answer is %d\n", x.the_answer()); ➎ printf("%s member\n", x.member); ➏ // This line doesn't compile: // printf("%s's Holistic Detective Agency\n", x.holistic_detective); ➐ }
it still would not compile. It has nothing to do with inheritance, but with the fact that holistic_detective is a private member of x.
A better example would be showing that you can't call holistic_detective within derived class itself. A bit of discussion about protected and private would make sense there as well.

@JLospinoso
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants