1 // { dg-do assemble } 2 // GROUPS passed visibility 3 // visibility file 4 // From: Gordon Joly <G.Joly@cs.ucl.ac.uk> 5 // Date: Wed, 21 Apr 93 09:42:07 +0100 6 // Subject: /*** BUG REPORT : THE MYTH OF PRIVATE INHERITANCE ***/ 7 // Message-ID: <9304210842.AA01815@life.ai.mit.edu> 8 #include <iostream> 9 10 class A { 11 private: 12 int number; 13 public: A(int i)14 A(int i) : number(i) 15 {} ~A()16 virtual ~A() 17 {} Number(int c)18 virtual void Number(int c) // { dg-message "declared" } 19 { number = c; } Number()20 virtual int Number() // { dg-message "declared" } 21 { return number; } 22 }; 23 24 class B : private A { 25 private: 26 int second_number; 27 public: B(int c,int i)28 B(int c, int i) : second_number(c), A(i) 29 {} ~B()30 virtual ~B() 31 {} 32 firstNumber(int b)33 virtual void firstNumber(int b) // renames member function Number(int) of class A 34 { A::Number(b); } firstNumber()35 virtual int firstNumber() // renames member function Number() of class A 36 { return A::Number(); } 37 }; 38 39 40 41 42 class C { 43 private: 44 B* bobject; 45 public: C(B * bp)46 C(B* bp) : bobject(bp) 47 {} ~C()48 virtual ~C() 49 {} 50 // 51 // the following two functions access 52 // private member functions of class B 53 // and they should not be able to do so 54 // setBValue(int i)55 virtual void setBValue(int i) 56 { if (bobject) bobject->Number(i); } // { dg-error "this context|accessible base" } getBValue()57 virtual int getBValue() 58 { if (bobject) { return bobject->Number(); } return 0; } // { dg-error "this context|accessible base" } 59 }; 60 61 main()62int main() 63 { 64 B* bobject = new B(2, 1); 65 C* cobject = new C(bobject); 66 cobject->setBValue(8); 67 std::cout << cobject->getBValue() << std::endl; 68 delete bobject; 69 delete cobject; 70 } 71 72 73 74