c++ - Error with private inheritance
- Sean Wall (38/38) Jan 29 2004 I think I found a bug in DMC 8.38.9n with regards to default inheritance...
I think I found a bug in DMC 8.38.9n with regards to default inheritance access specifiers. This came up as I was running some code from Bruce Eckel's book. Given this code: ----------------------------------------------------- class Pet { public: char eat() const { return 'a'; } int speak() const { return 2; } float sleep() const { return 3.0; } float sleep(int) const { return 4.0; } }; class Goldfish : Pet { // should be default private inheritance // because Goldfish is defined as a 'class' public: using Pet::eat; // Name publicizes member using Pet::sleep; // Both members exposed }; int main() { Goldfish bob; bob.eat(); bob.sleep(); bob.sleep(1); bob.speak(); // Error: private member function shouldn't be visible } -------------------------------------------------------- The compiler should give an error for the line 'bob.speak();' because class Goldfish inherits from class Pet using the default access specifier, which is 'private' for classes. However, the statement 'bob.speak();' compiles with no error and runs. In this regard, 'class Goldfish' is being treated by the compiler like it's actually 'struct Goldfish', where the default inheritance access specifier is 'public'. Making this modification: .. class Goldfish : private Pet { .. does make the compiler flag the 'bob.speak();' statement as an error (as it should). -Sean
Jan 29 2004