www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript

c++ - Error with private 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